home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / SOUND / MP3CONV / !MP3Conv / c / decode < prev    next >
Text File  |  1998-04-09  |  95KB  |  3,227 lines

  1. /**********************************************************************
  2.  * ISO MPEG Audio Subgroup Software Simulation Group (1996)
  3.  * ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
  4.  *
  5.  * $Id: decode.c,v 1.2 1996/03/28 03:13:37 rowlands Exp $
  6.  *
  7.  * $Log: decode.c,v $
  8.  * Revision 1.2  1996/03/28 03:13:37  rowlands
  9.  * Merged layers 1-2 and layer 3 revisions
  10.  *
  11.  * Revision 1.1  1996/02/14 03:45:52  rowlands
  12.  * Initial revision
  13.  *
  14.  * Received from FhG
  15.  **********************************************************************/
  16. /**********************************************************************
  17.  *   date   programmers         comment                               *
  18.  * 2/25/91  Douglas Wong,       start of version 1.0 records          *
  19.  *          Davis Pan                                                 *
  20.  * 3/06/91  Douglas Wong        rename: setup.h to dedef.h            *
  21.  *                                      dfilter to defilter           *
  22.  *                                      dwindow to dewindow           *
  23.  *                              integrated "quantizer", "scalefactor" *
  24.  *                              combined window_samples routine into  *
  25.  *                              filter samples                        *
  26.  * 3/31/91  Bill Aspromonte     replaced read_filter by               *
  27.  *                              create_syn_filter and introduced a    *
  28.  *                              new Sub-Band Synthesis routine called *
  29.  *                              SubBandSynthesis()                    *
  30.  * 5/10/91  Vish (PRISM)        Ported to Macintosh and Unix.         *
  31.  *                              Changed "out_fifo()" so that last     *
  32.  *                              unfilled block is also written out.   *
  33.  *                              "create_syn_filter()" was modified so *
  34.  *                              that calculation precision is same as *
  35.  *                              in specification tables.              *
  36.  *                              Changed "decode_scale()" to reflect   *
  37.  *                              specifications.                       *
  38.  *                              Removed all routines used by          *
  39.  *                              "synchronize_buffer()".  This is now  *
  40.  *                              replaced by "seek_sync()".            *
  41.  *                              Incorporated Jean-Georges Fritsch's   *
  42.  *                              "bitstream.c" package.                *
  43.  *                              Deleted "reconstruct_sample()".       *
  44.  * 27jun91  dpwe (Aware)        Passed outFile and &sampFrames as     *
  45.  *                              args to out_fifo() - were global.     *
  46.  *                              Moved "alloc_*" reader to common.c.   *
  47.  *                              alloc, sblimit, stereo passed via new *
  48.  *                              'frame_params struct (were globals).  *
  49.  *                              Added JOINT STEREO decoding, lyrs I&II*
  50.  *                              Affects: decode_bitalloc,buffer_samps *
  51.  *                              Plus a few other cleanups.            *
  52.  * 6/10/91   Earle Jennings     conditional expansion added in        *
  53.  *                              II_dequantize_sample to handle range  *
  54.  *                              problems in MSDOS version             *
  55.  * 8/8/91    Jens Spille        Change for MS-C6.00                   *
  56.  *10/1/91    S.I. Sudharsanan,  Ported to IBM AIX platform.           *
  57.  *           Don H. Lee,                                              *
  58.  *           Peter W. Farrett                                         *
  59.  *10/3/91    Don H. Lee         implemented CRC-16 error protection   *
  60.  *                              newly introduced functions are        *
  61.  *                              buffer_CRC and recover_CRC_error.     *
  62.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  63.  *                              important fixes involved changing     *
  64.  *                              16-bit ints to long or unsigned in    *
  65.  *                              bit alloc routines for quant of 65535 *
  66.  *                              and passing proper function args.     *
  67.  *                              Removed "Other Joint Stereo" option   *
  68.  *                              and made bitrate be total channel     *
  69.  *                              bitrate, irrespective of the mode.    *
  70.  *                              Fixed many small bugs & reorganized.  *
  71.  * 7/27/92  Juan Pineda         Bug fix in SubBandSynthesis()         *
  72.  *--------------------------------------------------------------------*
  73.  * 6/14/92  Juan Pineda         Layer III decoding routines added.    *
  74.  *          Amit Gulati         Follows CD 3-11172 rev2.  Contains    *
  75.  *                              hacks deal with evolving available    *
  76.  *                              layerIII bitstreams.  Some (minor)    *
  77.  *                              modification of prior LI&II code.     *
  78.  * 10/25/92 Amit Gulati         Updated layerIII routines. Added code *
  79.  *                              for subblock_gain, switched block     *
  80.  *                              modes, stereo pre-processing.         *
  81.  *                              Corrected sign bits for huffman       *
  82.  *                              decoding of quadruples region and     *
  83.  *                              adjusted gain factor in III_dequant.  *
  84.  * 11/21/92 Amit Gulati         Several layerIII bugs fixed.          *
  85.  * 12/15/92 Amit Gulati         Corrected reordering (indexing)       *
  86.  *          Stan Searing        within IMDCT routine.                 *
  87.  *  8/24/93 Masahiro Iwadare    Included IS modification in Layer III.*
  88.  *                              Changed for 1 pass decoding.          *
  89.  *  9/07/93 Toshiyuki Ishino    Integrated Layer III with Ver 3.9.    *
  90.  *--------------------------------------------------------------------*
  91.  * 11/20/93 Masahiro Iwadare    Integrated Layer III with Ver 4.0.    *
  92.  *--------------------------------------------------------------------*
  93.  *  7/14/94 Juergen Koller      Bug fixes in Layer III code           *
  94.  *--------------------------------------------------------------------*
  95.  * 08/11/94 IIS                 Bug fixes in Layer III code           *
  96.  *--------------------------------------------------------------------*
  97.  * 9/20/94  Davis Pan           Modification to avoid premature       *
  98.  *                              synchword detection                   *
  99.  *--------------------------------------------------------------------*
  100.  * 11/09/94 Jon Rowlands        Merged premature synchword detection  *
  101.  *                              fix into layer III code version       *
  102.  *--------------------------------------------------------------------*
  103.  * 07/12/95 Soeren H. Nielsen   Changes for LSF Layer I and II        *
  104.  *--------------------------------------------------------------------*
  105.  *    8/95  Roland Bitto    Adapted to MPEG2                      *
  106.  *  9/8/95  Roalnd Bitto        Bugfix in Function III_stereo         *
  107.  *--------------------------------------------------------------------*
  108.  * 12/16/96 Johan Hagman    Adapted for Solaris (mpeg3play 0.9)   *
  109.  *--------------------------------------------------------------------*
  110.  * 04/09/98 Niall Douglas    Adapted for RISC-OS                   *
  111.  **********************************************************************/
  112.  
  113. #include        "common.h"
  114. #include        "decoder.h"
  115. #include        "huffman.h"
  116. #include        "sound.h"
  117.  
  118. /***************************************************************
  119.  *
  120.  * This module contains the core of the decoder ie all the
  121.  * computational routines. (Layer I and II only)
  122.  * Functions are common to both layer unless
  123.  * otherwise specified.
  124.  *
  125.  ***************************************************************/
  126.  
  127. extern Arguments_t Arguments;
  128. extern int       audiofd;
  129.  
  130. /*****************************************************************
  131.  *
  132.  * The following routines decode the system information
  133.  *
  134.  ****************************************************************/
  135.  
  136. /************ Layer I, Layer II & Layer III ******************/
  137.  
  138. void decode_info(
  139.     Bit_stream_struc    *bs,
  140.     frame_params    *fr_ps)
  141. {
  142.     layer *hdr = fr_ps->header;
  143.  
  144.     hdr->version    = get1bit(bs);
  145.     hdr->lay        = 4 - getbits(bs,2);
  146.     hdr->error_protection = !get1bit(bs); /* error protect. TRUE/FALSE */
  147.     hdr->bitrate_index    = getbits(bs,4);
  148.     hdr->sampling_frequency = getbits(bs,2);
  149.     hdr->padding    = get1bit(bs);
  150.     hdr->extension    = get1bit(bs);
  151.     hdr->mode = getbits(bs,2);
  152.     hdr->mode_ext    = getbits(bs,2);
  153.     hdr->copyright    = get1bit(bs);
  154.     hdr->original    = get1bit(bs);
  155.     hdr->emphasis    = getbits(bs,2);
  156. }
  157.  
  158. /*******************************************************************
  159.  *
  160.  * The bit allocation information is decoded. Layer I
  161.  * has 4 bit per subband whereas Layer II is Ws and bit rate
  162.  * dependent.
  163.  *
  164.  ********************************************************************/
  165.  
  166. /**************************** Layer II *************/
  167.  
  168. void II_decode_bitalloc(
  169.     Bit_stream_struc    *bs,
  170.     unsigned int     bit_alloc[2][SBLIMIT],
  171.     frame_params    *fr_ps)
  172. {
  173.     int         i, j;
  174.     int         stereo = fr_ps->stereo;
  175.     int         sblimit = fr_ps->sblimit;
  176.     int         jsbound = fr_ps->jsbound;
  177.     al_table    *alloc = fr_ps->alloc;
  178.  
  179.     for (i = 0; i < jsbound; i++)
  180.     for (j = 0; j < stereo; j++)
  181.         bit_alloc[j][i] = (char)getbits(bs, (*alloc)[i][0].bits);
  182.  
  183.     for (i = jsbound; i < sblimit; i++)        /* expand to 2 channels */
  184.     bit_alloc[0][i] = bit_alloc[1][i] =
  185.             (char)getbits(bs, (*alloc)[i][0].bits);
  186.  
  187.     for (i = sblimit; i < SBLIMIT; i++)
  188.     for (j = 0; j < stereo; j++)
  189.         bit_alloc[j][i] = 0;
  190. }
  191.  
  192. /**************************** Layer I *************/
  193.  
  194. void I_decode_bitalloc(
  195.     Bit_stream_struc *bs,
  196.     unsigned int bit_alloc[2][SBLIMIT],
  197.     frame_params *fr_ps)
  198. {
  199.     int i,j;
  200.     int stereo    = fr_ps->stereo;
  201.     /*int sblimit = fr_ps->sblimit;*/
  202.     int jsbound = fr_ps->jsbound;
  203.     int b;
  204.  
  205.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  206.         bit_alloc[j][i] = getbits(bs,4);
  207.     for (i=jsbound;i<SBLIMIT;i++) {
  208.         b = getbits(bs,4);
  209.         for (j=0;j<stereo;j++)
  210.             bit_alloc[j][i] = b;
  211.     }
  212. }
  213.  
  214. /*****************************************************************
  215.  *
  216.  * The following two functions implement the layer I and II
  217.  * format of scale factor extraction. Layer I involves reading
  218.  * 6 bit per subband as scale factor. Layer II requires reading
  219.  * first the scfsi which in turn indicate the number of scale factors
  220.  * transmitted.
  221.  *    Layer I : I_decode_scale
  222.  *   Layer II : II_decode_scale
  223.  *
  224.  ****************************************************************/
  225.  
  226. /************************** Layer I stuff ************************/
  227.  
  228. void I_decode_scale(
  229.     Bit_stream_struc *bs, unsigned int bit_alloc[2][SBLIMIT],
  230.     unsigned int scale_index[2][3][SBLIMIT], frame_params *fr_ps)
  231. {
  232.     int i,j;
  233.     int stereo = fr_ps->stereo;
  234.     /*int sblimit = fr_ps->sblimit;*/
  235.  
  236.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  237.         if (!bit_alloc[j][i])
  238.             scale_index[j][0][i] = SCALE_RANGE-1;
  239.         else                    /* 6 bit per scale factor */
  240.             scale_index[j][0][i] =  getbits(bs,6);
  241.  
  242. }
  243.  
  244. /*************************** Layer II stuff ***************************/
  245.  
  246. void II_decode_scale(
  247.     Bit_stream_struc    *bs,
  248.     unsigned int     scfsi[2][SBLIMIT],
  249.     unsigned int     bit_alloc[2][SBLIMIT],
  250.     unsigned int     scale_index[2][3][SBLIMIT],
  251.     frame_params    *fr_ps)
  252. {
  253.     int        i, j;
  254.     int        stereo = fr_ps->stereo;
  255.     int        sblimit = fr_ps->sblimit;
  256.  
  257. #ifdef OPTIMIZE
  258.  
  259.     for (i = 0; i < sblimit; i++)
  260.     for (j = 0; j < stereo; j++) {        /* 2 bit scfsi */
  261.         if (bit_alloc[j][i])
  262.         scfsi[j][i] = (char)getbits(bs,2);
  263.         else
  264.         scfsi[j][i] = 0;
  265.     }
  266.  
  267.     for (i = 0; i < sblimit; i++) {
  268.     for (j = 0; j < stereo; j++) {
  269.         if (bit_alloc[j][i])
  270.         switch (scfsi[j][i]) {
  271.         case 0 :
  272.             /* All three scale factors transmitted */
  273.             scale_index[j][0][i] = getbits(bs,6);
  274.             scale_index[j][1][i] = getbits(bs,6);
  275.             scale_index[j][2][i] = getbits(bs,6);
  276.             break;
  277.         case 1 :
  278.             /* Scale factor 1 & 3 transmitted */
  279.             scale_index[j][0][i] = scale_index[j][1][i] = getbits(bs,6);
  280.             scale_index[j][2][i] = getbits(bs,6);
  281.             break;
  282.         case 3 :
  283.             /* Scale factor 1 & 2 transmitted */
  284.             scale_index[j][0][i] = getbits(bs,6);
  285.             scale_index[j][1][i] = scale_index[j][2][i] = getbits(bs,6);
  286.             break;
  287.         case 2 :
  288.             /* Only one scale factor transmitted */
  289.             scale_index[j][0][i] = scale_index[j][1][i] =
  290.             scale_index[j][2][i] = getbits(bs,6);
  291.             break;
  292.         default :
  293.             break;
  294.         }
  295.         else {
  296.         scale_index[j][0][i] = scale_index[j][1][i] =
  297.             scale_index[j][2][i] = SCALE_RANGE-1;
  298.         }
  299.     }
  300.     }
  301.     for (i = sblimit; i < SBLIMIT; i++)
  302.     for (j = 0; j < stereo; j++)
  303.         scale_index[j][0][i] = scale_index[j][1][i] =
  304.             scale_index[j][2][i] = SCALE_RANGE - 1;
  305.  
  306. #else
  307.     /* Original code */
  308.  
  309.     for (i = 0; i < sblimit; i++)
  310.     for (j = 0; j < stereo; j++)        /* 2 bit scfsi */
  311.         if (bit_alloc[j][i])
  312.         scfsi[j][i] = (char)getbits(bs,2);
  313.  
  314.     for (i = sblimit; i < SBLIMIT; i++)
  315.     for (j = 0; j < stereo; j++)
  316.         scfsi[j][i] = 0;
  317.  
  318.     for (i = 0; i < sblimit; i++) {
  319.     for (j = 0; j < stereo; j++) {
  320.         if (bit_alloc[j][i])
  321.         switch (scfsi[j][i]) {
  322.         case 0 :
  323.             /* All three scale factors transmitted */
  324.             scale_index[j][0][i] = getbits(bs,6);
  325.             scale_index[j][1][i] = getbits(bs,6);
  326.             scale_index[j][2][i] = getbits(bs,6);
  327.             break;
  328.         case 1 :
  329.             /* Scale factor 1 & 3 transmitted */
  330.             scale_index[j][0][i] = scale_index[j][1][i] = getbits(bs,6);
  331.             scale_index[j][2][i] = getbits(bs,6);
  332.             break;
  333.         case 3 :
  334.             /* Scale factor 1 & 2 transmitted */
  335.             scale_index[j][0][i] = getbits(bs,6);
  336.             scale_index[j][1][i] = scale_index[j][2][i] = getbits(bs,6);
  337.             break;
  338.         case 2 :
  339.             /* Only one scale factor transmitted */
  340.             scale_index[j][0][i] = scale_index[j][1][i] =
  341.             scale_index[j][2][i] = getbits(bs,6);
  342.             break;
  343.         default :
  344.             break;
  345.         }
  346.         else {
  347.         scale_index[j][0][i] = scale_index[j][1][i] =
  348.             scale_index[j][2][i] = SCALE_RANGE-1;
  349.         }
  350.     }
  351.     }
  352.  
  353.     for (i = sblimit; i < SBLIMIT; i++)
  354.     for (j = 0; j < stereo; j++)
  355.         scale_index[j][0][i] = scale_index[j][1][i] =
  356.             scale_index[j][2][i] = SCALE_RANGE - 1;
  357. #endif /* OPTIMIZE */
  358. }
  359.  
  360. /**************************************************************
  361.  *
  362.  *   The following two routines take care of reading the
  363.  * compressed sample from the bit stream for both layer 1 and
  364.  * layer 2. For layer 1, read the number of bits as indicated
  365.  * by the bit_alloc information. For layer 2, if grouping is
  366.  * indicated for a particular subband, then the sample size has
  367.  * to be read from the bits_group and the merged samples has
  368.  * to be decompose into the three distinct samples. Otherwise,
  369.  * it is the same for as layer one.
  370.  *
  371.  **************************************************************/
  372.  
  373. /******************************* Layer I stuff ******************/
  374.  
  375. void I_buffer_sample(
  376.     Bit_stream_struc *bs,
  377.     unsigned int FAR sample[2][3][SBLIMIT],
  378.     unsigned int bit_alloc[2][SBLIMIT],
  379.     frame_params *fr_ps)
  380. {
  381.     int i,j,k;
  382.     int stereo = fr_ps->stereo;
  383.     /*int sblimit = fr_ps->sblimit;*/
  384.     int jsbound = fr_ps->jsbound;
  385.     unsigned int s;
  386.  
  387.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  388.         if ( (k = bit_alloc[j][i]) == 0)
  389.             sample[j][0][i] = 0;
  390.         else
  391.             sample[j][0][i] = (unsigned int) getbits(bs,k+1);
  392.     for (i=jsbound;i<SBLIMIT;i++) {
  393.         if ( (k = bit_alloc[0][i]) == 0)
  394.             s = 0;
  395.         else
  396.             s = (unsigned int)getbits(bs,k+1);
  397.         for (j=0;j<stereo;j++)
  398.             sample[j][0][i]    = s;
  399.     }
  400. }
  401.  
  402. /*************************** Layer II stuff ************************/
  403.  
  404. void II_buffer_sample(
  405.     Bit_stream_struc    *bs,
  406.     unsigned int FAR     sample[2][3][SBLIMIT],
  407.     unsigned int     bit_alloc[2][SBLIMIT],
  408.     frame_params    *fr_ps)
  409. {
  410.     int         i, j;
  411. #ifndef OPTIMIZE
  412.     int         m;
  413. #endif
  414.     int         stereo = fr_ps->stereo;
  415.     int         sblimit = fr_ps->sblimit;
  416.     int         jsbound = fr_ps->jsbound;
  417.     al_table    *alloc = fr_ps->alloc;
  418.  
  419. #ifdef OPTIMIZE
  420.  
  421.     for (i = 0; i < sblimit; i++)
  422.     for (j = 0; j < ((i < jsbound) ? stereo : 1); j++) {
  423.         if (bit_alloc[j][i]) {
  424.  
  425.         /* Check for grouping in subband */
  426.         int k = (*alloc)[i][bit_alloc[j][i]].bits;
  427.  
  428.         if ((*alloc)[i][bit_alloc[j][i]].group == 3) {
  429.  
  430.             sample[j][0][i] = (unsigned int)getbits(bs, k);
  431.             sample[j][1][i] = (unsigned int)getbits(bs, k);
  432.             sample[j][2][i] = (unsigned int)getbits(bs, k);
  433.  
  434.         } else {
  435.             /* bit_alloc = 3, 5, 9 */
  436.             unsigned int nlevels, c;
  437.  
  438.             nlevels = (*alloc)[i][bit_alloc[j][i]].steps;
  439.             c = (unsigned int)getbits(bs, k);
  440.             /* Unrolled loop */
  441.             sample[j][0][i] = c % nlevels;
  442.             /* Assign both at once. Note that c is modified!! */
  443.             sample[j][1][i] = (c /= nlevels) % nlevels;
  444.             sample[j][2][i] = (c /= nlevels) % nlevels;
  445.         }
  446.         } else {
  447.         /* For no sample transmitted */
  448.         sample[j][0][i] = sample[j][1][i] = sample[j][2][i] = 0;
  449.         }
  450.         if (stereo == 2 && i >= jsbound) {    /* joint stereo: copy L to R */
  451.         sample[1][0][i] = sample[0][0][i];
  452.         sample[1][1][i] = sample[0][1][i];
  453.         sample[1][2][i] = sample[0][2][i];
  454.         }
  455.     }
  456.  
  457. #else
  458.     /* Original code */
  459.     int    k;
  460.  
  461.     for (i = 0; i < sblimit; i++)
  462.     for (j = 0; j < ((i<jsbound) ? stereo : 1); j++) {
  463.         if (bit_alloc[j][i]) {
  464.         /* Check for grouping in subband */
  465.         if ((*alloc)[i][bit_alloc[j][i]].group == 3)
  466.             for (m = 0; m < 3; m++) {
  467.             k = (*alloc)[i][bit_alloc[j][i]].bits;
  468.             sample[j][m][i] = (unsigned int)getbits(bs, k);
  469.             }
  470.         else {
  471.             /* bit_alloc = 3, 5, 9 */
  472.             unsigned int nlevels,
  473.                  c = 0;
  474.  
  475.             nlevels = (*alloc)[i][bit_alloc[j][i]].steps;
  476.             k = (*alloc)[i][bit_alloc[j][i]].bits;
  477.             c = (unsigned int)getbits(bs, k);
  478.             for (k = 0; k < 3; k++) {
  479.             sample[j][k][i] = c % nlevels;
  480.             c /= nlevels;
  481.             }
  482.         }
  483.         } else {
  484.         /* For no sample transmitted */
  485.         for (k = 0; k < 3; k++)
  486.             sample[j][k][i] = 0;
  487.         }
  488.         if (stereo == 2 && i>= jsbound)    /* joint stereo: copy L to R */
  489.         for (k = 0; k < 3; k++)
  490.             sample[1][k][i] = sample[0][k][i];
  491.     }
  492.  
  493.     for (i = sblimit; i < SBLIMIT; i++)
  494.     for (j = 0; j < stereo; j++)
  495.         for (k = 0; k < 3; k++)
  496.         sample[j][k][i] = 0;
  497. #endif /* OPTIMIZE */
  498. }
  499.  
  500. /**************************************************************
  501.  *
  502.  *   Restore the compressed sample to a fractional number.
  503.  *   first complement the MSB of the sample
  504.  *    for layer I :
  505.  *    Use s = (s' + 2^(-nb+1) ) * 2^nb / (2^nb-1)
  506.  *   for Layer II :
  507.  *   Use the formula s = s' * c + d
  508.  *
  509.  **************************************************************/
  510.  
  511. static REAL c[17] = {    1.33333333333, 1.60000000000, 1.14285714286,
  512.             1.77777777777, 1.06666666666, 1.03225806452,
  513.             1.01587301587, 1.00787401575, 1.00392156863,
  514.             1.00195694716, 1.00097751711, 1.00048851979,
  515.             1.00024420024, 1.00012208522, 1.00006103888,
  516.             1.00003051851, 1.00001525902 };
  517.  
  518. static REAL d[17] = {    0.500000000, 0.500000000, 0.250000000, 0.500000000,
  519.             0.125000000, 0.062500000, 0.031250000, 0.015625000,
  520.             0.007812500, 0.003906250, 0.001953125, 0.0009765625,
  521.             0.00048828125, 0.00024414063, 0.00012207031,
  522.             0.00006103516, 0.00003051758 };
  523.  
  524. /************************** Layer II stuff ************************/
  525.  
  526.  
  527. #ifndef ASM_OPTIMIZE
  528.  
  529. void II_dequantize_sample(
  530.     unsigned int FAR     sample[2][3][SBLIMIT],
  531.     unsigned int     bit_alloc[2][SBLIMIT],
  532.     REAL FAR         fraction[2][3][SBLIMIT],
  533.     frame_params    *fr_ps)
  534. {
  535.     int         i, j, k;
  536.     int         stereo = fr_ps->stereo;
  537.     int         sblimit = fr_ps->sblimit;
  538.     al_table    *alloc = fr_ps->alloc;
  539.  
  540. #ifdef OPTIMIZE
  541.  
  542.     for (i = 0; i < sblimit; i++)
  543.     for (j = 0; j < 3; j++) {
  544.         for (k = 0; k < stereo; k++) {
  545.         REAL *f = &fraction[k][j][i];
  546.  
  547.         if (bit_alloc[k][i]) {
  548.             int  x = 0;
  549.             REAL r;
  550.             /*
  551.              * Locate MSB in the sample
  552.              * u is set outside loop to hopefully speed up the loop
  553.              */
  554.             unsigned int u = (*alloc)[i][bit_alloc[k][i]].steps;
  555.             while (((unsigned int) 1 << x) < u)
  556.             x++;
  557.  
  558.             /* MSB inversion */
  559.             u = sample[k][j][i];    /* read sample once */
  560.  
  561.             x = 1L << (x - 1);        /* for MSB test */
  562.  
  563.             if ((u & x))
  564.             r = 0.0;        /* MSB is set */
  565.             else
  566.             r = -1.0;        /* MSB is clear */
  567.  
  568.             /*
  569.              * Form a 2's complement sample
  570.              * This is a slow operation
  571.              */
  572.             r += (REAL)(u & (x - 1)) / (REAL)x;
  573.  
  574.             u = (*alloc)[i][bit_alloc[k][i]].quant;
  575.  
  576.             /* Dequantize the sample */
  577.             r += d[u];
  578.             r *= c[u];
  579.             *f = r;        /* destination touched once */
  580.  
  581.         } else
  582.             *f = 0.0;
  583.         }
  584.     }
  585.  
  586. #else /* original code */
  587.  
  588.     int x;
  589.  
  590.     for (i = 0; i < sblimit; i++)
  591.     for (j = 0; j < 3; j++)
  592.         for (k = 0; k < stereo; k++)
  593.         if (bit_alloc[k][i]) {
  594.             /* Locate MSB in the sample */
  595.             x = 0;
  596. #ifndef MS_DOS
  597.             while ((1L<<x) < (*alloc)[i][bit_alloc[k][i]].steps)
  598.             x++;
  599. #else
  600.             /* Microsoft C thinks an int is a short */
  601.             while (( (unsigned long) (1L<<(long)x) <
  602.                 (unsigned long)((*alloc)[i][bit_alloc[k][i]].steps))
  603.                 && ( x < 16) )
  604.             x++;
  605. #endif
  606.             /* MSB inversion */
  607.             if (((sample[k][j][i] >> x-1) & 1) == 1)
  608.             fraction[k][j][i] = 0.0;
  609.             else
  610.             fraction[k][j][i] = -1.0;
  611.  
  612.             /* Form a 2's complement sample */
  613.             fraction[k][j][i] +=
  614.             (REAL)(sample[k][j][i] & ((1<<x-1)-1)) /
  615.             (REAL)(1L<<x-1);
  616.  
  617.             /* Dequantize the sample */
  618.             fraction[k][j][i] += d[(*alloc)[i][bit_alloc[k][i]].quant];
  619.             fraction[k][j][i] *= c[(*alloc)[i][bit_alloc[k][i]].quant];
  620.         } else
  621.             fraction[k][j][i] = 0.0;
  622.  
  623.     for (i = sblimit; i < SBLIMIT; i++)
  624.     for (j = 0; j < 3; j++)
  625.         for (k = 0; k < stereo; k++)
  626.         fraction[k][j][i] = 0.0;
  627.  
  628. #endif /* OPTIMIZE */
  629. }
  630. #endif /* ASM_OPTIMIZE */
  631.  
  632.  
  633. /***************************** Layer I stuff ***********************/
  634.  
  635. void I_dequantize_sample(
  636.     unsigned int FAR sample[2][3][SBLIMIT],
  637.     REAL FAR fraction[2][3][SBLIMIT],
  638.     unsigned int bit_alloc[2][SBLIMIT],
  639.     frame_params *fr_ps)
  640. {
  641.     int i, nb, k;
  642.     int stereo = fr_ps->stereo;
  643.     /*int sblimit = fr_ps->sblimit;*/
  644.  
  645.     for (i=0; i<SBLIMIT; i++)
  646.     for (k=0; k<stereo; k++)
  647.         if (bit_alloc[k][i]) {
  648.         nb = bit_alloc[k][i] + 1;
  649.         if (((((sample[k][0][i]) >> nb) - 1) & 1) == 1)
  650.             fraction[k][0][i] = 0.0;
  651.         else
  652.             fraction[k][0][i] = -1.0;
  653.  
  654.         fraction[k][0][i] += (sample[k][0][i] & (((1 << nb) - 1) - 1)) /
  655.                      ((1L << nb) - 1);
  656.  
  657.         fraction[k][0][i] =
  658.                 (fraction[k][0][i] + 1.0 / ((1L << nb) - 1)) *
  659.                 (1L<<nb) / ((1L<<nb)-1);
  660.         } else
  661.         fraction[k][0][i] = 0.0;
  662. }
  663.  
  664. /************************************************************
  665.  *
  666.  *   Restore the original value of the sample ie multiply
  667.  *    the fraction value by its scalefactor.
  668.  *
  669.  ************************************************************/
  670.  
  671. /************************* Layer II Stuff **********************/
  672.  
  673. void II_denormalize_sample(
  674.     REAL FAR         fraction[2][3][SBLIMIT],
  675.     unsigned int     scale_index[2][3][SBLIMIT],
  676.     frame_params    *fr_ps,
  677.     int             x)
  678. {
  679.     int        i, j;
  680.     int        stereo = fr_ps->stereo;
  681.     int        sblimit = fr_ps->sblimit;
  682.  
  683. #ifdef OPTIMIZE
  684.  
  685.     for (i = 0; i < sblimit; i++)
  686.     for (j = 0; j < stereo; j++) {
  687.         REAL m = multiple[scale_index[j][x][i]];
  688.  
  689.         fraction[j][0][i] *= m;
  690.         fraction[j][1][i] *= m;
  691.         fraction[j][2][i] *= m;
  692.     }
  693.  
  694. #else /* original code */
  695.  
  696.     for (i = 0; i < sblimit; i++)
  697.     for (j = 0; j < stereo; j++) {
  698.         fraction[j][0][i] *= multiple[scale_index[j][x][i]];
  699.         fraction[j][1][i] *= multiple[scale_index[j][x][i]];
  700.         fraction[j][2][i] *= multiple[scale_index[j][x][i]];
  701.     }
  702. #endif /* OPTIMIZE */
  703. }
  704.  
  705. /**************************** Layer I stuff ******************************/
  706.  
  707. void I_denormalize_sample(
  708.     REAL FAR fraction[2][3][SBLIMIT],
  709.     unsigned int scale_index[2][3][SBLIMIT],
  710.     frame_params *fr_ps)
  711. {
  712.     int i, j;
  713.     int stereo = fr_ps->stereo;
  714.  
  715.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  716.         fraction[j][0][i] *= multiple[scale_index[j][0][i]];
  717. }
  718.  
  719. /*****************************************************************
  720.  *
  721.  * The following are the subband synthesis routines. They apply
  722.  * to both layer I and layer II stereo or mono. The user has to
  723.  * decide what parameters are to be passed to the routines.
  724.  *
  725.  ***************************************************************/
  726.  
  727. /*************************************************************
  728.  *
  729.  *   Pass the subband sample through the synthesis window
  730.  *
  731.  **************************************************************/
  732.  
  733. /* create in synthesis filter */
  734.  
  735. void create_syn_filter(REAL FAR filter[64][SBLIMIT])
  736. {
  737.     int i, k;
  738. #ifdef NO_MODFF
  739.     double temp;
  740. #endif
  741.  
  742.     for (i = 0; i < 64; i++)
  743.     for (k = 0; k < 32; k++) {
  744. #ifdef FSINGLE
  745.         filter[i][k] = 1e9 * cos((PI64*i+PI4)*(2.0*k+1.0));
  746.         if (filter[i][k] >= 0) {
  747.         /*modff(filter[i][k]+0.5, &filter[i][k]);*/
  748.         filter[i][k] = floor(filter[i][k] + 0.5);
  749.         } else {
  750.         /*modff(filter[i][k]-0.5, &filter[i][k]);*/
  751.         filter[i][k] = floor(filter[i][k] - 0.5);
  752.         }
  753. #else /* FSINGLE */
  754.         filter[i][k] = 1e9*cos((PI64*i+PI4)*(2*k+1));
  755.         if (filter[i][k] >= 0)
  756.         /*modf(filter[i][k]+0.5, &filter[i][k]);*/
  757.         filter[i][k] = floor(filter[i][k] + 0.5);
  758.         else
  759.         /*modf(filter[i][k]-0.5, &filter[i][k]);*/
  760.         filter[i][k] = floor(filter[i][k] - 0.5);
  761. #endif
  762.         filter[i][k] *= 1e-9;
  763.     }
  764.     /*
  765.      * filter[][] is now filled with values between -1 and 1.
  766.      */
  767. }
  768.  
  769.  
  770. #ifndef BUILTIN_TABLES
  771.  
  772. /***************************************************************
  773.  *
  774.  *   Window the restored sample
  775.  *
  776.  ***************************************************************/
  777.  
  778. /* read in synthesis window */
  779.  
  780. void read_syn_window(REAL FAR window[HAN_SIZE])
  781. {
  782.     int      i, j[4];
  783.     FILE *fp;
  784.     REAL  f[4];
  785.     char  t[150];
  786.  
  787.     if (!(fp = OpenTableFile("dewindow") )) {
  788.         fprintf(stderr, "Please check synthesis window table 'dewindow'\n");
  789.         exit(1);
  790.     }
  791.     for (i=0; i<512; i+=4) {
  792.         fgets(t, 150, fp);
  793. #ifdef FSINGLE
  794.     sscanf(t,"D[%d] = %f D[%d] = %f D[%d] = %f D[%d] = %f\n",
  795.         j, f, j+1, f+1, j+2, f+2, j+3, f+3);
  796. #else
  797.         sscanf(t,"D[%d] = %lf D[%d] = %lf D[%d] = %lf D[%d] = %lf\n",
  798.         j, f, j+1, f+1, j+2, f+2, j+3, f+3);
  799. #endif
  800.         if (i == j[0]) {
  801.             window[i] = f[0];
  802.             window[i+1] = f[1];
  803.             window[i+2] = f[2];
  804.             window[i+3] = f[3];
  805.         }
  806.         else {
  807.             fprintf(stderr, "Check index in synthesis window table\n");
  808.             exit(1);
  809.         }
  810.         fgets(t, 150, fp);
  811.     }
  812.     fclose(fp);
  813. }
  814.  
  815. #endif /* BUILTIN_TABLES */
  816.  
  817.  
  818. #ifndef INT_MATH
  819.  
  820. /* Set to 1 for the optimization level from version 0.9.3 and earlier */
  821. #define OPTIMIZE_093    0
  822. /* Set to 1 for the "fuzzy zero detection" optimization in version 0.9.5 */
  823. #define OPTIMIZE_095    0
  824.  
  825. int SubBandSynthesis(
  826.     REAL    *bandPtr,
  827.     int         channel,
  828.     short    *samples)
  829. {
  830.     int             i, j;
  831.     REAL        *bufOffsetPtr, sum;
  832.     static int         init = 1;
  833.     typedef REAL     NN[64][32];
  834.     static NN FAR    *filter;
  835.     typedef REAL     BB[2][2*HAN_SIZE];
  836.     static BB FAR    *buf;
  837.     static int         bufOffset[2] = {64,64};
  838.     static REAL FAR    *window;
  839.     int             clip = 0;   /* count & return # samples clipped */
  840.     int             offset;
  841.     long         foo;
  842.     int             k;
  843. #ifdef OPTIMIZE
  844.     REAL        *filterp;
  845.     REAL        *bandp;
  846. # if OPTIMIZE_095
  847.     int             nzbcount, nzbands[32];    /* non-zero band detection */
  848. # endif
  849. #endif
  850.  
  851.     if (init == 1) {
  852.         buf = (BB FAR *) mem_alloc(sizeof(BB),"BB");
  853.         filter = (NN FAR *) mem_alloc(sizeof(NN), "NN");
  854.         create_syn_filter(*filter);
  855.         window = (REAL FAR *) mem_alloc(sizeof(REAL) * HAN_SIZE, "WIN");
  856.         read_syn_window(window);
  857.     init = 0;
  858.     }
  859.  
  860.     bufOffset[channel] = (bufOffset[channel] - 64) & 0x3ff;
  861.     bufOffsetPtr = &((*buf)[channel][bufOffset[channel]]);
  862.  
  863. #ifdef OPTIMIZE
  864.  
  865. # if OPTIMIZE_093
  866.  
  867.     filterp = (REAL *)((*filter)[0]);
  868.     bandp = bandPtr;
  869.  
  870.     i = 64;
  871.     while (i--) {
  872.  
  873.     *bufOffsetPtr++ =
  874.         bandp[0] * filterp[0] +
  875.         bandp[1] * filterp[1] +
  876.         bandp[2] * filterp[2] +
  877.         bandp[3] * filterp[3] +
  878.         bandp[4] * filterp[4] +
  879.         bandp[5] * filterp[5] +
  880.         bandp[6] * filterp[6] +
  881.         bandp[7] * filterp[7] +
  882.         bandp[8] * filterp[8] +
  883.         bandp[9] * filterp[9] +
  884.         bandp[10] * filterp[10] +
  885.         bandp[11] * filterp[11] +
  886.         bandp[12] * filterp[12] +
  887.         bandp[13] * filterp[13] +
  888.         bandp[14] * filterp[14] +
  889.         bandp[15] * filterp[15] +
  890.         bandp[16] * filterp[16] +
  891.         bandp[17] * filterp[17] +
  892.         bandp[18] * filterp[18] +
  893.         bandp[19] * filterp[19] +
  894.         bandp[20] * filterp[20] +
  895.         bandp[21] * filterp[21] +
  896.         bandp[22] * filterp[22] +
  897.         bandp[23] * filterp[23] +
  898.         bandp[24] * filterp[24] +
  899.         bandp[25] * filterp[25] +
  900.         bandp[26] * filterp[26] +
  901.         bandp[27] * filterp[27] +
  902.         bandp[28] * filterp[28] +
  903.         bandp[29] * filterp[29] +
  904.         bandp[30] * filterp[30] +
  905.         bandp[31] * filterp[31];
  906.  
  907.     filterp += 32;
  908.     }
  909.  
  910. # elif OPTIMIZE_095
  911.  
  912.     filterp = (REAL *)((*filter)[0]);
  913.     bandp = bandPtr;
  914.  
  915.     /* Analyze input */
  916.     for (i = 0, nzbcount = 0; i < 32; i++)
  917.     /* Fuzzy zero detection */
  918.     if (bandp[i] < -0.00035 || bandp[i] > 0.00035)
  919.         nzbands[nzbcount++] = i;
  920.  
  921.     for (i = 0; i < 64; i++, filterp += 32) {
  922.  
  923.     /* Only add non-zero bands */
  924.     for (j = 0, sum = 0.0; j < nzbcount; j++)
  925.         sum += bandp[nzbands[j]] * filterp[nzbands[j]];
  926.  
  927.     *bufOffsetPtr++ = sum;
  928.     }
  929.  
  930. # else /*optimize using a fast cosine transform */
  931.  
  932. #define cos1_64  0.500602998235196
  933. #define cos3_64  0.505470959897544
  934. #define cos5_64  0.515447309922625
  935. #define cos7_64  0.531042591089784
  936. #define cos9_64  0.553103896034445
  937. #define cos11_64 0.582934968206134
  938. #define cos13_64 0.622504123035665
  939. #define cos15_64 0.674808341455006
  940. #define cos17_64 0.744536271002299
  941. #define cos19_64 0.839349645415527
  942. #define cos21_64 0.972568237861961
  943. #define cos23_64 1.169439933432885
  944. #define cos25_64 1.484164616314166
  945. #define cos27_64 2.057781009953411
  946. #define cos29_64 3.407608418468719
  947. #define cos31_64 10.190008123548033
  948. #define cos1_32  0.502419286188156
  949. #define cos3_32  0.522498614939689
  950. #define cos5_32  0.566944034816358
  951. #define cos7_32  0.646821783359990
  952. #define cos9_32  0.788154623451250
  953. #define cos11_32 1.060677685990347
  954. #define cos13_32 1.722447098238334
  955. #define cos15_32 5.101148618689155
  956. #define cos1_16  0.509795579104159
  957. #define cos3_16  0.601344886935045
  958. #define cos5_16  0.899976223136416
  959. #define cos7_16  2.562915447741505
  960. #define cos1_8   0.541196100146197
  961. #define cos3_8   1.306562964876376
  962. #define cos1_4   0.707106781186547
  963.  
  964.   {
  965.     int i;
  966.     register REAL tmp;
  967.     REAL p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15;
  968.     REAL pp0,pp1,pp2,pp3,pp4,pp5,pp6,pp7,pp8,pp9,pp10,pp11,pp12,pp13,pp14,pp15;
  969.  
  970.     /* Compute new values via a fast cosine transform */
  971.     p0  = bandPtr[0] + bandPtr[31];
  972.     p1  = bandPtr[1] + bandPtr[30];
  973.     p2  = bandPtr[2] + bandPtr[29];
  974.     p3  = bandPtr[3] + bandPtr[28];
  975.     p4  = bandPtr[4] + bandPtr[27];
  976.     p5  = bandPtr[5] + bandPtr[26];
  977.     p6  = bandPtr[6] + bandPtr[25];
  978.     p7  = bandPtr[7] + bandPtr[24];
  979.     p8  = bandPtr[8] + bandPtr[23];
  980.     p9  = bandPtr[9] + bandPtr[22];
  981.     p10 = bandPtr[10]+ bandPtr[21];
  982.     p11 = bandPtr[11]+ bandPtr[20];
  983.     p12 = bandPtr[12]+ bandPtr[19];
  984.     p13 = bandPtr[13]+ bandPtr[18];
  985.     p14 = bandPtr[15]+ bandPtr[17];
  986.     p15 = bandPtr[16]+ bandPtr[16];
  987.  
  988.     pp0 = p0 + p15;
  989.     pp1 = p1 + p14;
  990.     pp2 = p2 + p13;
  991.     pp3 = p3 + p12;
  992.     pp4 = p4 + p11;
  993.     pp5 = p5 + p10;
  994.     pp6 = p6 + p9;
  995.     pp7 = p7 + p8;
  996.     pp8 = cos1_32  * (p0 - p15);
  997.     pp9 = cos3_32  * (p1 - p14);
  998.     pp10= cos5_32  * (p2 - p13);
  999.     pp11= cos7_32  * (p3 - p12);
  1000.     pp12= cos9_32  * (p4 - p11);
  1001.     pp13= cos11_32 * (p5 - p10);
  1002.     pp14= cos13_32 * (p6 - p9);
  1003.     pp15= cos15_32 * (p7 - p8);
  1004.  
  1005.     p0 = pp0 + pp7;
  1006.     p1 = pp1 + pp6;
  1007.     p2 = pp2 + pp5;
  1008.     p3 = pp3 + pp4;
  1009.     p4 = cos1_16 * (pp0 - pp7);
  1010.     p5 = cos3_16 * (pp1 - pp6);
  1011.     p6 = cos5_16 * (pp2 - pp5);
  1012.     p7 = cos7_16 * (pp3 - pp4);
  1013.     p8 = pp8 + pp15;
  1014.     p9 = pp9 + pp14;
  1015.     p10= pp10 + pp13;
  1016.     p11= pp11 + pp12;
  1017.     p12= cos1_16 * (pp8  - pp15);
  1018.     p13= cos3_16 * (pp9  - pp14);
  1019.     p14= cos5_16 * (pp10 - pp13);
  1020.     p15= cos7_16 * (pp11 - pp12);
  1021.  
  1022.     pp0 = p0 + p3;
  1023.     pp1 = p1 + p2;
  1024.     pp2 = cos1_8 * (p0 - p3);
  1025.     pp3 = cos3_8 * (p1 - p2);
  1026.     pp4 = p4 + p7;
  1027.     pp5 = p5 + p6;
  1028.     pp6 = cos1_8 * (p4 - p7);
  1029.     pp7 = cos3_8 * (p5 - p6);
  1030.     pp8 = p8 + p11;
  1031.     pp9 = p9 + p10;
  1032.     pp10= cos1_8 * (p8 - p11);
  1033.     pp11= cos3_8 * (p9 - p10);
  1034.     pp12= p12 + p15;
  1035.     pp13 = p13 + p14;
  1036.     pp14= cos1_8 * (p12 - p15);
  1037.     pp15= cos3_8 * (p13 - p14);
  1038.  
  1039.     p0 = pp0 + pp1;
  1040.     p1 = cos1_4 * (pp0 - pp1);
  1041.     p2 = pp2 + pp3;
  1042.     p3 = cos1_4 * (pp2 - pp3);
  1043.     p4 = pp4 + pp5;
  1044.     p5 = cos1_4 * (pp4 - pp5);
  1045.     p6 = pp6 + pp7;
  1046.     p7 = cos1_4 * (pp6 - pp7);
  1047.     p8 = pp8 + pp9;
  1048.     p9 = cos1_4 * (pp8 - pp9);
  1049.     p10= pp10 + pp11;
  1050.     p11= cos1_4 * (pp10 - pp11);
  1051.     p12= pp12 + pp13;
  1052.     p13= cos1_4 * (pp12 - pp13);
  1053.     p14= pp14 + pp15;
  1054.     p15= cos1_4 * (pp14 - pp15);
  1055.  
  1056.     tmp              = p6 + p7;
  1057.     bufOffsetPtr[36] = -(p5 + tmp);
  1058.     bufOffsetPtr[44] = -(p4 + tmp);
  1059.     tmp              = p11 + p15;
  1060.     bufOffsetPtr[10] = tmp;
  1061.     bufOffsetPtr[6]  = p13 + tmp;
  1062.     tmp              = p14 + p15;
  1063.     bufOffsetPtr[46] = -(p8  + p12 + tmp);
  1064.     bufOffsetPtr[34] = -(p9  + p13 + tmp);
  1065.     tmp             += p10 + p11;
  1066.     bufOffsetPtr[38] = -(p13 + tmp);
  1067.     bufOffsetPtr[42] = -(p12 + tmp);
  1068.     bufOffsetPtr[2]  = p9 + p13 + p15;
  1069.     bufOffsetPtr[4]  = p5 + p7;
  1070.     bufOffsetPtr[48] = -p0;
  1071.     bufOffsetPtr[0]  = p1;
  1072.     bufOffsetPtr[8]  = p3;
  1073.     bufOffsetPtr[12] = p7;
  1074.     bufOffsetPtr[14] = p15;
  1075.     bufOffsetPtr[40] = -(p2  + p3);
  1076.  
  1077.     p0  = cos1_64  * (bandPtr[0] - bandPtr[31]);
  1078.     p1  = cos3_64  * (bandPtr[1] - bandPtr[30]);
  1079.     p2  = cos5_64  * (bandPtr[2] - bandPtr[29]);
  1080.     p3  = cos7_64  * (bandPtr[3] - bandPtr[28]);
  1081.     p4  = cos9_64  * (bandPtr[4] - bandPtr[27]);
  1082.     p5  = cos11_64 * (bandPtr[5] - bandPtr[26]);
  1083.     p6  = cos13_64 * (bandPtr[6] - bandPtr[25]);
  1084.     p7  = cos15_64 * (bandPtr[7] - bandPtr[24]);
  1085.     p8  = cos17_64 * (bandPtr[8] - bandPtr[23]);
  1086.     p9  = cos19_64 * (bandPtr[9] - bandPtr[22]);
  1087.     p10 = cos21_64 * (bandPtr[10]- bandPtr[21]);
  1088.     p11 = cos23_64 * (bandPtr[11]- bandPtr[20]);
  1089.     p12 = cos25_64 * (bandPtr[12]- bandPtr[19]);
  1090.     p13 = cos27_64 * (bandPtr[13]- bandPtr[18]);
  1091.     p14 = cos29_64 * (bandPtr[14]- bandPtr[17]);
  1092.     p15 = cos31_64 * (bandPtr[15]- bandPtr[16]);
  1093.  
  1094.     pp0 = p0 + p15;
  1095.     pp1 = p1 + p14;
  1096.     pp2 = p2 + p13;
  1097.     pp3 = p3 + p12;
  1098.     pp4 = p4 + p11;
  1099.     pp5 = p5 + p10;
  1100.     pp6 = p6 + p9;
  1101.     pp7 = p7 + p8;
  1102.     pp8 = cos1_32  * (p0 - p15);
  1103.     pp9 = cos3_32  * (p1 - p14);
  1104.     pp10= cos5_32  * (p2 - p13);
  1105.     pp11= cos7_32  * (p3 - p12);
  1106.     pp12= cos9_32  * (p4 - p11);
  1107.     pp13= cos11_32 * (p5 - p10);
  1108.     pp14= cos13_32 * (p6 - p9);
  1109.     pp15= cos15_32 * (p7 - p8);
  1110.  
  1111.     p0 = pp0 + pp7;
  1112.     p1 = pp1 + pp6;
  1113.     p2 = pp2 + pp5;
  1114.     p3 = pp3 + pp4;
  1115.     p4 = cos1_16 * (pp0 - pp7);
  1116.     p5 = cos3_16 * (pp1 - pp6);
  1117.     p6 = cos5_16 * (pp2 - pp5);
  1118.     p7 = cos7_16 * (pp3 - pp4);
  1119.     p8 = pp8  + pp15;
  1120.     p9 = pp9  + pp14;
  1121.     p10= pp10 + pp13;
  1122.     p11= pp11 + pp12;
  1123.     p12= cos1_16 * (pp8  - pp15);
  1124.     p13= cos3_16 * (pp9  - pp14);
  1125.     p14= cos5_16 * (pp10 - pp13);
  1126.     p15= cos7_16 * (pp11 - pp12);
  1127.  
  1128.     pp0 = p0 + p3;
  1129.     pp1 = p1 + p2;
  1130.     pp2 = cos1_8 * (p0 - p3);
  1131.     pp3 = cos3_8 * (p1 - p2);
  1132.     pp4 = p4 + p7;
  1133.     pp5 = p5 + p6;
  1134.     pp6 = cos1_8 * (p4 - p7);
  1135.     pp7 = cos3_8 * (p5 - p6);
  1136.     pp8 = p8 + p11;
  1137.     pp9 = p9 + p10;
  1138.     pp10= cos1_8 * (p8 - p11);
  1139.     pp11= cos3_8 * (p9 - p10);
  1140.     pp12= p12 + p15;
  1141.     pp13= p13 + p14;
  1142.     pp14= cos1_8 * (p12 - p15);
  1143.     pp15= cos3_8 * (p13 - p14);
  1144.  
  1145.     p0 = pp0 + pp1;
  1146.     p1 = cos1_4 * (pp0 - pp1);
  1147.     p2 = pp2 + pp3;
  1148.     p3 = cos1_4 * (pp2 - pp3);
  1149.     p4 = pp4 + pp5;
  1150.     p5 = cos1_4 * (pp4 - pp5);
  1151.     p6 = pp6 + pp7;
  1152.     p7 = cos1_4 * (pp6 - pp7);
  1153.     p8 = pp8 + pp9;
  1154.     p9 = cos1_4 * (pp8 - pp9);
  1155.     p10= pp10 + pp11;
  1156.     p11= cos1_4 * (pp10 - pp11);
  1157.     p12= pp12 + pp13;
  1158.     p13= cos1_4 * (pp12 - pp13);
  1159.     p14= pp14 + pp15;
  1160.     p15= cos1_4 * (pp14 - pp15);
  1161.  
  1162.     tmp              = p13 + p15;
  1163.     bufOffsetPtr[1]  = p1 + p9 + tmp;
  1164.     bufOffsetPtr[5]  = p5 + p7 + p11 + tmp;
  1165.     tmp             += p9;
  1166.     bufOffsetPtr[33] = -(p1 + p14 + tmp);
  1167.     tmp             += p5 + p7;
  1168.     bufOffsetPtr[3]  = tmp;
  1169.     bufOffsetPtr[35] = -(p6 + p14 + tmp);
  1170.     tmp              = p10 + p11 + p12 + p13 + p14 + p15;
  1171.     bufOffsetPtr[39] = -(p2 + p3 + tmp - p12);
  1172.     bufOffsetPtr[43] = -(p4 + p6 + p7 + tmp - p13);
  1173.     bufOffsetPtr[37] = -(p5 + p6 + p7 + tmp - p12);
  1174.     bufOffsetPtr[41] = -(p2 + p3 + tmp - p13);
  1175.     tmp              = p8 + p12 + p14 + p15;
  1176.     bufOffsetPtr[47] = -(p0 + tmp);
  1177.     bufOffsetPtr[45] = -(p4 + p6 + p7 + tmp);
  1178.     tmp              = p11 + p15;
  1179.     bufOffsetPtr[11] = p7  + tmp;
  1180.     tmp             += p3;
  1181.     bufOffsetPtr[9]  = tmp;
  1182.     bufOffsetPtr[7]  = p13 + tmp;
  1183.     bufOffsetPtr[13] = p7 + p15;
  1184.     bufOffsetPtr[15] = p15;
  1185.  
  1186.     bufOffsetPtr[16] = 0.0;
  1187.     for (i = 0; i < 16; i++) {
  1188.     bufOffsetPtr[32-i] = -bufOffsetPtr[i];
  1189.     bufOffsetPtr[63-i] = bufOffsetPtr[33+i];
  1190.     }
  1191.   }
  1192.  
  1193. # endif /* OPTIMIZE_093 & OPTIMIZE_095 */
  1194.  
  1195. #else /* original code */
  1196.  
  1197.     for (i = 0; i < 64; i++) {
  1198.     sum = 0.0;
  1199.     for (k = 0; k < 32; k++)
  1200.         sum += bandPtr[k] * (*filter)[i][k];
  1201.     bufOffsetPtr[i] = sum;
  1202.     }
  1203. #endif /* OPTIMIZE */
  1204.  
  1205.  
  1206. # if OPTIMIZE_095 /* code in version 0.9.5 and earlier */
  1207.  
  1208. #  ifdef DETECT_CLIP
  1209.     /* Detects and "smooths" clipping
  1210.      *
  1211.      *  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)
  1212.      *  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)
  1213.      */
  1214.  
  1215.     for (j = 0; j < 32; j++) {    /* for each subband... */
  1216.     offset = bufOffset[channel] + j;
  1217.     /*
  1218.      * -1.144989014 <= window[x] <= 1.144989014  for all x
  1219.      */
  1220.     sum = window[j] * (*buf)[channel][offset] +
  1221.         window[j + 32] * (*buf)[channel][(96 + offset) & 0x3ff] +
  1222.         window[j + 64] * (*buf)[channel][(128 + offset) & 0x3ff] +
  1223.         window[j + 96] * (*buf)[channel][(224 + offset) & 0x3ff] +
  1224.         window[j + 128] * (*buf)[channel][(256 + offset) & 0x3ff] +
  1225.         window[j + 160] * (*buf)[channel][(352 + offset) & 0x3ff] +
  1226.         window[j + 192] * (*buf)[channel][(384 + offset) & 0x3ff] +
  1227.         window[j + 224] * (*buf)[channel][(480 + offset) & 0x3ff] +
  1228.         window[j + 256] * (*buf)[channel][(512 + offset) & 0x3ff] +
  1229.         window[j + 288] * (*buf)[channel][(608 + offset) & 0x3ff] +
  1230.         window[j + 320] * (*buf)[channel][(640 + offset) & 0x3ff] +
  1231.         window[j + 352] * (*buf)[channel][(736 + offset) & 0x3ff] +
  1232.         window[j + 384] * (*buf)[channel][(768 + offset) & 0x3ff] +
  1233.         window[j + 416] * (*buf)[channel][(864 + offset) & 0x3ff] +
  1234.         window[j + 448] * (*buf)[channel][(896 + offset) & 0x3ff] +
  1235.         window[j + 480] * (*buf)[channel][(992 + offset) & 0x3ff];
  1236.  
  1237.     /*
  1238.      * At this point we hope we have -1 <= sum <= 1 otherwise, it'll
  1239.      * have to be clipped anyway, so it's no big deal to clip it earlier
  1240.      * or something.
  1241.      */
  1242.  
  1243.     /*
  1244.      * Casting truncates towards zero for both positive and negative
  1245.      * numbers, the result is cross-over distortion,  1995-07-12 shn
  1246.      */
  1247.  
  1248.     if (sum > 0) {
  1249.         foo = (long)(sum * SCALE + 0.5);
  1250.     } else {
  1251.         foo = (long)(sum * SCALE - 0.5);
  1252.     }
  1253.  
  1254.     if (foo >= (long)SCALE) {
  1255.         samples[j] = (short)(SCALE-1);
  1256.         ++clip;
  1257.     } else if (foo < (long)-SCALE) {
  1258.         samples[j] = (short)(-SCALE);
  1259.         ++clip;
  1260.     } else
  1261.         samples[j] = (short)foo;
  1262.     }
  1263.  
  1264. #  else /* !DETECT_CLIP */
  1265.  
  1266.     /* Faster, but does not detect clipping
  1267.      *
  1268.      *  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)
  1269.      *  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)
  1270.      */
  1271.     for (j = 0; j < 32; j++) {
  1272.     sum = 0.0;
  1273.  
  1274.     for (i = 0; i < 16; i++) {
  1275.         k = j + (i << 5);
  1276.         sum += window[k] * (*buf)[channel]
  1277.             [((k + (((i+1) >> 1) << 6)) + bufOffset[channel]) & 0x3ff];
  1278.     }
  1279.     samples[j] = sum * SCALE;
  1280.     }
  1281.  
  1282. #  endif /* DETECT_CLIP */
  1283.  
  1284. # else /* most optimized */
  1285.  
  1286.   {
  1287.     REAL* dt[16];
  1288.     for (i = 0; i < 16; i++) {
  1289.     k = (i << 5);
  1290.     dt[i]=(*buf)[channel] + ( ((k + (((i+1) >> 1) << 6)) +
  1291.                 bufOffset[channel]) & 0x3ff);
  1292.     }
  1293.  
  1294.     for (j = 0; j < 32; j++) {
  1295.     /* Unroll loop
  1296.      * Here we multiply every subband by the appropriate scale
  1297.      * factor and add 'em all up, so we get one sample.
  1298.      */
  1299.     sum =
  1300.         window[j] * dt[0][j]+
  1301.         window[j + 32] * dt[1][j]+
  1302.         window[j + 64] * dt[2][j]+
  1303.         window[j + 96] * dt[3][j]+
  1304.         window[j + 128] * dt[4][j]+
  1305.         window[j + 160] * dt[5][j]+
  1306.         window[j + 192] * dt[6][j]+
  1307.         window[j + 224] * dt[7][j]+
  1308.         window[j + 256] * dt[8][j]+
  1309.         window[j + 288] * dt[9][j]+
  1310.         window[j + 320] * dt[10][j]+
  1311.         window[j + 352] * dt[11][j]+
  1312.         window[j + 384] * dt[12][j]+
  1313.         window[j + 416] * dt[13][j]+
  1314.         window[j + 448] * dt[14][j]+
  1315.         window[j + 480] * dt[15][j];
  1316.  
  1317.     /*foo = floor(sum * SCALE);*/
  1318.     if (sum > 0) {
  1319.         foo = (long)(sum * SCALE + 0.5);
  1320.     } else {
  1321.         foo = (long)(sum * SCALE - 0.5);
  1322.     }
  1323.  
  1324. #  ifdef DETECT_CLIP
  1325.     if (foo >= (long)SCALE) {
  1326.         samples[j] = (short)(SCALE-1);
  1327.         ++clip;
  1328.     } else if (foo < (long)-SCALE) {
  1329.         samples[j] = (short)(-SCALE);
  1330.         ++clip;
  1331.     } else
  1332. #  endif
  1333.       samples[j] = foo;
  1334.       }
  1335.     }
  1336.  
  1337. # endif /* OPTIMIZE_095 */
  1338.  
  1339.     return clip;
  1340. }
  1341.  
  1342. #else /* INT_MATH */
  1343.  
  1344. int SubBandSynthesis(
  1345.     REAL    *bandPtr,
  1346.     int         channel,
  1347.     short    *samples)
  1348. {
  1349.     int             i, j, k;
  1350.     int             sum;
  1351.     int            *bufOffsetPtr;
  1352.     static int         init = 1;
  1353.     typedef REAL     RNN[64][32];
  1354.     static RNN FAR    *Rfilter;
  1355.     typedef int         NN[64][32];
  1356.     static NN FAR    *filter;
  1357.     typedef int         BB[2][2*HAN_SIZE];
  1358.     static BB FAR    *buf;
  1359.     static int         bufOffset[2] = {64,64};
  1360.     static int        *window;
  1361.     static REAL FAR    *realWindow;
  1362.     int             clip = 0;   /* count & return # samples clipped */
  1363.     long         foo;
  1364.     int             offset;
  1365.     int            *filterp;
  1366.     REAL        *bandp;
  1367.  
  1368.     if (init == 1) {
  1369.     buf = (BB FAR *) mem_alloc(sizeof(BB),"BB");
  1370.  
  1371.     filter = (NN FAR *) mem_alloc(sizeof(NN), "NN");
  1372.     Rfilter = (RNN FAR *) mem_alloc(sizeof(RNN), "RNN");
  1373.     create_syn_filter(*Rfilter);
  1374.     for (i = 0; i < 64; i++) {
  1375.         for (j = 0; j < 32; j++) {
  1376.         (*filter)[i][j] = (*Rfilter)[i][j] * 32768;
  1377.         }
  1378.     }
  1379.  
  1380.     window = (int *) mem_alloc(sizeof(int) * HAN_SIZE, "WIN");
  1381.     realWindow = (REAL FAR *) mem_alloc(sizeof(REAL FAR) * HAN_SIZE, "WIN");
  1382.     read_syn_window(realWindow);
  1383.     /*
  1384.      * Let's now convert those nasty REALs to nice 'n fast ints.
  1385.      * This should really be done in read_syn_window() but for now this
  1386.      * at least keeps all the bugs in one place.
  1387.      */
  1388.     for (i = 0; i < HAN_SIZE; i++) {
  1389.         window[i] = realWindow[i] * 32768;
  1390.     }
  1391.  
  1392.     init = 0;
  1393.     }
  1394.  
  1395.     bufOffset[channel] = (bufOffset[channel] - 64) & 0x3ff;
  1396.     bufOffsetPtr = &((*buf)[channel][bufOffset[channel]]);
  1397.  
  1398.     filterp = (int *)((*filter)[0]);
  1399.     bandp = bandPtr;
  1400.  
  1401.     i = 64;
  1402.  
  1403.     while (i--) {
  1404.     /*
  1405.      * -1 <= filter <= 1
  1406.      */
  1407.     *bufOffsetPtr++ =
  1408.         bandp[0] * filterp[0] +
  1409.         bandp[1] * filterp[1] +
  1410.         bandp[2] * filterp[2] +
  1411.         bandp[3] * filterp[3] +
  1412.         bandp[4] * filterp[4] +
  1413.         bandp[5] * filterp[5] +
  1414.         bandp[6] * filterp[6] +
  1415.         bandp[7] * filterp[7] +
  1416.         bandp[8] * filterp[8] +
  1417.         bandp[9] * filterp[9] +
  1418.         bandp[10] * filterp[10] +
  1419.         bandp[11] * filterp[11] +
  1420.         bandp[12] * filterp[12] +
  1421.         bandp[13] * filterp[13] +
  1422.         bandp[14] * filterp[14] +
  1423.         bandp[15] * filterp[15] +
  1424.         bandp[16] * filterp[16] +
  1425.         bandp[17] * filterp[17] +
  1426.         bandp[18] * filterp[18] +
  1427.         bandp[19] * filterp[19] +
  1428.         bandp[20] * filterp[20] +
  1429.         bandp[21] * filterp[21] +
  1430.         bandp[22] * filterp[22] +
  1431.         bandp[23] * filterp[23] +
  1432.         bandp[24] * filterp[24] +
  1433.         bandp[25] * filterp[25] +
  1434.         bandp[26] * filterp[26] +
  1435.         bandp[27] * filterp[27] +
  1436.         bandp[28] * filterp[28] +
  1437.         bandp[29] * filterp[29] +
  1438.         bandp[30] * filterp[30] +
  1439.         bandp[31] * filterp[31];
  1440.     filterp += 32;
  1441.     }
  1442.  
  1443.     /* Detects and "smooths" clipping
  1444.      *
  1445.      *  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)
  1446.      *  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)
  1447.      */
  1448.  
  1449.     for (j = 0; j < 32; j++) {    /* for each subband... */
  1450.     offset = bufOffset[channel] + j;
  1451.     sum =
  1452.        (window[j] * (*buf)[channel][offset] +
  1453.         window[j + 32] * (*buf)[channel][(96 + offset) & 0x3ff] +
  1454.         window[j + 64] * (*buf)[channel][(128 + offset) & 0x3ff] +
  1455.         window[j + 96] * (*buf)[channel][(224 + offset) & 0x3ff] +
  1456.         window[j + 128] * (*buf)[channel][(256 + offset) & 0x3ff] +
  1457.         window[j + 160] * (*buf)[channel][(352 + offset) & 0x3ff] +
  1458.         window[j + 192] * (*buf)[channel][(384 + offset) & 0x3ff] +
  1459.         window[j + 224] * (*buf)[channel][(480 + offset) & 0x3ff] +
  1460.         window[j + 256] * (*buf)[channel][(512 + offset) & 0x3ff] +
  1461.         window[j + 288] * (*buf)[channel][(608 + offset) & 0x3ff] +
  1462.         window[j + 320] * (*buf)[channel][(640 + offset) & 0x3ff] +
  1463.         window[j + 352] * (*buf)[channel][(736 + offset) & 0x3ff] +
  1464.         window[j + 384] * (*buf)[channel][(768 + offset) & 0x3ff] +
  1465.         window[j + 416] * (*buf)[channel][(864 + offset) & 0x3ff] +
  1466.         window[j + 448] * (*buf)[channel][(896 + offset) & 0x3ff] +
  1467.         window[j + 480] * (*buf)[channel][(992 + offset) & 0x3ff]) / 32768;
  1468.  
  1469.     /*
  1470.      * Casting truncates towards zero for both positive and negative
  1471.      * numbers, the result is cross-over distortion,  1995-07-12 shn
  1472.      */
  1473.  
  1474.     if (sum >= (long)SCALE) {
  1475.         samples[j] = (short)(SCALE-1);
  1476.         ++clip;
  1477.     } else if (sum < (long)-SCALE) {
  1478.         samples[j] = (short)(-SCALE);
  1479.         ++clip;
  1480.     } else
  1481.         samples[j] = (short)sum;
  1482.     }
  1483.  
  1484.     return clip;
  1485. }
  1486.  
  1487. #endif /* INT_MATH */
  1488.  
  1489.  
  1490. /* Shift left and right channels for AIFF output */
  1491. static void swapWordsInLong(short *loc, int words)
  1492. {
  1493.     int             i;
  1494.     unsigned short     dst0, dst1;
  1495.     unsigned short    *src = (unsigned short*)loc;
  1496.  
  1497.     for (i = 0; i < words; i += 2) {
  1498.     dst0 = src[0];
  1499.     dst1 = src[1];
  1500.     *src++ = dst1;
  1501.     *src++ = dst0;
  1502.     }
  1503. }
  1504.  
  1505.  
  1506. void out_fifo(
  1507.     short FAR         pcm_sample[2][SSLIMIT][SBLIMIT],
  1508.     int             num,
  1509.     frame_params    *fr_ps,
  1510.     int             done,
  1511.     FILE        *outFile,
  1512.     unsigned long    *psampFrames)
  1513. {
  1514. #   define        BUFFERSIZE 8192        /* x 2 bytes */
  1515.     int            i, j, l;
  1516.     int            stereo = fr_ps->stereo;
  1517.     /*int        sblimit = fr_ps->sblimit;*/
  1518.     static short int    outsamp[BUFFERSIZE];
  1519.     static long        k = 0;
  1520.  
  1521.     if (!done) {
  1522.         for (i = 0; i < num; i++)
  1523.         for (j = 0; j < SBLIMIT; j++) {    /* SBLIMIT = 32 */
  1524.         (*psampFrames)++;
  1525.         for (l = 0; l < stereo; l++) {
  1526.             if (!(k & (BUFFERSIZE-1)) && k) {
  1527. #ifndef SOLARIS_SPARC
  1528.             /*
  1529.              * Samples are big-endian. If this is a little-endian
  1530.              * machine we must swap
  1531.              */
  1532.             if ( NativeByteOrder == order_unknown ) {
  1533.                 NativeByteOrder = DetermineByteOrder();
  1534.                 if ( NativeByteOrder == order_unknown ) {
  1535.                 fprintf(stderr, "byte order not determined\n" );
  1536.                 exit(1);
  1537.                 }
  1538.             }
  1539.             if ( NativeByteOrder == order_littleEndian )
  1540.                 SwapBytesInWords(outsamp, BUFFERSIZE);
  1541. #endif
  1542.             if (Arguments.write_to_file) {
  1543.                 if (stereo == 2) {
  1544.                 /* Swap channels for AIFF file */
  1545.                 swapWordsInLong(outsamp, BUFFERSIZE);
  1546.                 }
  1547.                 fwrite(outsamp, 2, BUFFERSIZE, outFile);
  1548.             } else
  1549.                 sound_write(audiofd, outsamp, BUFFERSIZE * 2);
  1550.             k = 0;
  1551.             }
  1552.             outsamp[k++] = pcm_sample[l][i][j];
  1553.         }
  1554.         }
  1555.  
  1556.     } else { /* if done */
  1557.  
  1558.     if (Arguments.write_to_file) {
  1559.         if (stereo == 2) {
  1560.         /* Swap channels for AIFF file */
  1561.         swapWordsInLong(outsamp, (int)k);
  1562.         }
  1563.         fwrite(outsamp, 2, (int)k, outFile);
  1564.     } else
  1565.         sound_write(audiofd, outsamp, (int)k * 2);
  1566.     k = 0;
  1567.     }
  1568. }
  1569.  
  1570.  
  1571. void buffer_CRC(Bit_stream_struc *bs, unsigned int *old_crc)
  1572. {
  1573.     *old_crc = getbits(bs, 16);
  1574. }
  1575.  
  1576.  
  1577. void recover_CRC_error(
  1578.     short FAR       pcm_sample[2][SSLIMIT][SBLIMIT],
  1579.     int           error_count,
  1580.     frame_params  *fr_ps,
  1581.     FILE      *outFile,
  1582.     unsigned long *psampFrames)
  1583. {
  1584.     int         stereo = fr_ps->stereo;
  1585.     int         num, done, i;
  1586.     int         samplesPerFrame, samplesPerSlot;
  1587.     layer    *hdr = fr_ps->header;
  1588.     long     offset;
  1589.     short    *temp;
  1590.  
  1591.     num = 3;
  1592.     if (hdr->lay == 1)
  1593.     num = 1;
  1594.  
  1595.     samplesPerSlot = SBLIMIT * num * stereo;
  1596.     samplesPerFrame = samplesPerSlot * 32;
  1597.  
  1598.     if (error_count == 1) {     /* replicate previous error_free frame */
  1599.         done = 1;
  1600.         /* Flush out fifo */
  1601.         out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  1602.         /* Go back to the beginning of the previous frame */
  1603.         offset = sizeof(short int) * samplesPerFrame;
  1604.         fseek(outFile, -offset, SEEK_CUR);
  1605.         done = 0;
  1606.         for (i = 0; i < SCALE_BLOCK; i++) {
  1607.             fread(pcm_sample, 2, samplesPerSlot, outFile);
  1608.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  1609.         }
  1610.  
  1611.     } else {                       /* mute the frame */
  1612.  
  1613.         temp = (short*)pcm_sample;
  1614.         done = 0;
  1615.         for (i = 0; i < 2*3*SBLIMIT; i++)
  1616.             *temp++ = MUTE;     /* MUTE value is in decoder.h */
  1617.         for (i = 0; i < SCALE_BLOCK; i++)
  1618.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  1619.     }
  1620. }
  1621.  
  1622. /************************* Layer III routines **********************/
  1623.  
  1624. void III_get_side_info(
  1625.     Bit_stream_struc *bs,
  1626.     III_side_info_t *si,
  1627.     frame_params *fr_ps)
  1628. {
  1629.    int    ch, gr, i;
  1630.    int    stereo = fr_ps->stereo;
  1631.  
  1632.    if (fr_ps->header->version != MPEG_PHASE2_LSF) {
  1633.     si->main_data_begin = getbits(bs, 9);
  1634.     if (stereo == 1)
  1635.         si->private_bits = getbits(bs,5);
  1636.     else
  1637.         si->private_bits = getbits(bs,3);
  1638.  
  1639.        for (ch = 0; ch < stereo; ch++)
  1640.         for (i = 0; i < 4; i++)
  1641.         si->ch[ch].scfsi[i] = get1bit(bs);
  1642.  
  1643.     for (gr = 0; gr < 2 ; gr++) {
  1644.         for (ch = 0; ch < stereo; ch++) {
  1645.         si->ch[ch].gr[gr].part2_3_length = getbits(bs, 12);
  1646.         si->ch[ch].gr[gr].big_values = getbits(bs, 9);
  1647.         si->ch[ch].gr[gr].global_gain = getbits(bs, 8);
  1648.         si->ch[ch].gr[gr].scalefac_compress = getbits(bs, 4);
  1649.         si->ch[ch].gr[gr].window_switching_flag = get1bit(bs);
  1650.         if (si->ch[ch].gr[gr].window_switching_flag) {
  1651.             si->ch[ch].gr[gr].block_type = getbits(bs, 2);
  1652.             si->ch[ch].gr[gr].mixed_block_flag = get1bit(bs);
  1653.             for (i = 0; i < 2; i++)
  1654.             si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);
  1655.             for (i = 0; i < 3; i++)
  1656.             si->ch[ch].gr[gr].subblock_gain[i] = getbits(bs, 3);
  1657.             /*
  1658.              * Set region_count parameters since they are
  1659.              * implicit in this case.
  1660.              */
  1661.             if (si->ch[ch].gr[gr].block_type == 0) {
  1662.             fprintf(stderr, "Side info bad: block_type 0 in split block\n");
  1663.             exit(0);
  1664.             } else if (si->ch[ch].gr[gr].block_type == 2
  1665.                 && si->ch[ch].gr[gr].mixed_block_flag == 0)
  1666.             si->ch[ch].gr[gr].region0_count = 8; /* MI 9; */
  1667.             else
  1668.             si->ch[ch].gr[gr].region0_count = 7; /* MI 8; */
  1669.             si->ch[ch].gr[gr].region1_count =
  1670.                 20 - si->ch[ch].gr[gr].region0_count;
  1671.         } else {
  1672.             for (i = 0; i < 3; i++)
  1673.             si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);
  1674.             si->ch[ch].gr[gr].region0_count = getbits(bs, 4);
  1675.             si->ch[ch].gr[gr].region1_count = getbits(bs, 3);
  1676.             si->ch[ch].gr[gr].block_type = 0;
  1677.         }
  1678.         si->ch[ch].gr[gr].preflag = get1bit(bs);
  1679.         si->ch[ch].gr[gr].scalefac_scale = get1bit(bs);
  1680.         si->ch[ch].gr[gr].count1table_select = get1bit(bs);
  1681.         }
  1682.     }
  1683.  
  1684.     } else {    /* Layer 3 LSF */
  1685.  
  1686.     si->main_data_begin = getbits(bs, 8);
  1687.     if (stereo == 1)
  1688.          si->private_bits = getbits(bs,1);
  1689.     else
  1690.         si->private_bits = getbits(bs,2);
  1691.  
  1692.     for (gr = 0; gr < 1 ; gr++) {
  1693.         for (ch = 0; ch < stereo; ch++) {
  1694.         si->ch[ch].gr[gr].part2_3_length = getbits(bs, 12);
  1695.         si->ch[ch].gr[gr].big_values = getbits(bs, 9);
  1696.         si->ch[ch].gr[gr].global_gain = getbits(bs, 8);
  1697.         si->ch[ch].gr[gr].scalefac_compress = getbits(bs, 9);
  1698.         si->ch[ch].gr[gr].window_switching_flag = get1bit(bs);
  1699.         if (si->ch[ch].gr[gr].window_switching_flag) {
  1700.             si->ch[ch].gr[gr].block_type = getbits(bs, 2);
  1701.             si->ch[ch].gr[gr].mixed_block_flag = get1bit(bs);
  1702.             for (i = 0; i < 2; i++)
  1703.             si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);
  1704.             for (i = 0; i < 3; i++)
  1705.             si->ch[ch].gr[gr].subblock_gain[i] = getbits(bs, 3);
  1706.             /*
  1707.              * Set region_count parameters since they are
  1708.              * implicit in this case.
  1709.              */
  1710.             if (si->ch[ch].gr[gr].block_type == 0) {
  1711.             fprintf(stderr, "Side info bad: block_type 0 in split block\n");
  1712.             exit(0);
  1713.             } else if (si->ch[ch].gr[gr].block_type == 2
  1714.                 && si->ch[ch].gr[gr].mixed_block_flag == 0)
  1715.             si->ch[ch].gr[gr].region0_count = 8; /* MI 9; */
  1716.             else
  1717.             si->ch[ch].gr[gr].region0_count = 7; /* MI 8; */
  1718.             si->ch[ch].gr[gr].region1_count =
  1719.                 20 - si->ch[ch].gr[gr].region0_count;
  1720.         } else {
  1721.             for (i = 0; i < 3; i++)
  1722.             si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);
  1723.             si->ch[ch].gr[gr].region0_count = getbits(bs, 4);
  1724.             si->ch[ch].gr[gr].region1_count = getbits(bs, 3);
  1725.             si->ch[ch].gr[gr].block_type = 0;
  1726.         }
  1727.  
  1728.         si->ch[ch].gr[gr].scalefac_scale = get1bit(bs);
  1729.         si->ch[ch].gr[gr].count1table_select = get1bit(bs);
  1730.         }
  1731.     }
  1732.     }
  1733. }
  1734.  
  1735.  
  1736. struct {
  1737.    int l[5];
  1738.    int s[3];} sfbtable = {{0, 6, 11, 16, 21},
  1739.                           {0, 6, 12}};
  1740.  
  1741. int slen[2][16] = {{0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
  1742.                    {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}};
  1743. struct  {
  1744.    int l[23];
  1745.    int s[14];} sfBandIndex[6] =
  1746.     {{{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  1747.      {0,4,8,12,18,24,32,42,56,74,100,132,174,192}},
  1748.     {{0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
  1749.      {0,4,8,12,18,26,36,48,62,80,104,136,180,192}},
  1750.     {{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  1751.      {0,4,8,12,18,26,36,48,62,80,104,134,174,192}},
  1752.  
  1753.     {{0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
  1754.      {0,4,8,12,16,22,30,40,52,66,84,106,136,192}},
  1755.     {{0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
  1756.      {0,4,8,12,16,22,28,38,50,64,80,100,126,192}},
  1757.     {{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
  1758.      {0,4,8,12,16,22,30,42,58,78,104,138,180,192}}};
  1759.  
  1760.  
  1761.  
  1762. void III_get_scale_factors(
  1763.     III_scalefac_t    *scalefac,
  1764.     III_side_info_t    *si,
  1765.     int             gr,
  1766.     int             ch,
  1767.     frame_params    *fr_ps)
  1768. {
  1769.     int             sfb, i, window;
  1770.     struct gr_info_s    *gr_info = &(si->ch[ch].gr[gr]);
  1771.  
  1772.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  1773.  
  1774.     if (gr_info->mixed_block_flag) {    /* MIXED */ /* NEW - ag 11/25 */
  1775.         for (sfb = 0; sfb < 8; sfb++)
  1776.         (*scalefac)[ch].l[sfb] =
  1777.             hgetbits(slen[0][gr_info->scalefac_compress]);
  1778.         for (sfb = 3; sfb < 6; sfb++)
  1779.         for (window=0; window<3; window++)
  1780.             (*scalefac)[ch].s[window][sfb] =
  1781.             hgetbits(slen[0][gr_info->scalefac_compress]);
  1782.         for (sfb = 6; sfb < 12; sfb++)
  1783.         for (window=0; window<3; window++)
  1784.             (*scalefac)[ch].s[window][sfb] =
  1785.             hgetbits(slen[1][gr_info->scalefac_compress]);
  1786.         for (sfb=12,window=0; window<3; window++)
  1787.         (*scalefac)[ch].s[window][sfb] = 0;
  1788.  
  1789.     } else {                /* SHORT*/
  1790.  
  1791.         for (i=0; i<2; i++)
  1792.         for (sfb = sfbtable.s[i]; sfb < sfbtable.s[i+1]; sfb++)
  1793.             for (window=0; window<3; window++)
  1794.             (*scalefac)[ch].s[window][sfb] =
  1795.                 hgetbits(slen[i][gr_info->scalefac_compress]);
  1796.         for (sfb=12,window=0; window<3; window++)
  1797.         (*scalefac)[ch].s[window][sfb] = 0;
  1798.     }
  1799.  
  1800.     } else {   /* LONG types 0,1,3 */
  1801.  
  1802.     for (i=0; i<4; i++) {
  1803.         if ((si->ch[ch].scfsi[i] == 0) || (gr == 0))
  1804.         for (sfb = sfbtable.l[i]; sfb < sfbtable.l[i+1]; sfb++)
  1805.             (*scalefac)[ch].l[sfb] =
  1806.               hgetbits(slen[(i<2)?0:1][gr_info->scalefac_compress]);
  1807.     }
  1808.     (*scalefac)[ch].l[22] = 0;
  1809.     }
  1810. }
  1811.  
  1812. /****************** new MPEG2 stuf  ***********/
  1813.  
  1814. static unsigned nr_of_sfb_block[6][3][4] = {{{6, 5, 5, 5},{ 9, 9, 9, 9 },{6, 9, 9, 9}},
  1815.                                          {{6, 5, 7, 3},{ 9, 9, 12, 6},{6, 9, 12, 6}},
  1816.                                          {{11, 10, 0, 0},{ 18, 18, 0, 0},{15,18,0,0 }},
  1817.                                          {{7, 7, 7, 0},{ 12, 12, 12, 0},{6, 15, 12, 0}},
  1818.                                          {{6, 6, 6, 3},{12, 9, 9, 6},{6, 12, 9, 6}},
  1819.                                          {{8, 8, 5, 0},{15,12,9,0},{6,18,9,0}}};
  1820. static unsigned scalefac_buffer[54];
  1821.  
  1822. void III_get_LSF_scale_data(
  1823.     III_scalefac_t *scalefac,
  1824.     III_side_info_t *si,
  1825.     int gr, int ch,
  1826.     frame_params *fr_ps)
  1827. {
  1828. short i, j, k;
  1829. short blocktypenumber, blocknumber = 0;
  1830.  
  1831. struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
  1832. unsigned scalefac_comp, int_scalefac_comp, new_slen[4];
  1833.  
  1834. layer *hdr = fr_ps->header;
  1835. scalefac_comp =  gr_info->scalefac_compress;
  1836.  
  1837.     blocktypenumber = 0;
  1838.     if ((gr_info->block_type == 2) && (gr_info->mixed_block_flag == 0))
  1839.                                                         blocktypenumber = 1;
  1840.  
  1841.    if ((gr_info->block_type == 2) && (gr_info->mixed_block_flag == 1))
  1842.                                                         blocktypenumber = 2;
  1843.  
  1844.     if(!((( hdr->mode_ext == 1) || (hdr->mode_ext == 3)) && (ch == 1)))
  1845.     {
  1846.     if(scalefac_comp < 400)
  1847.         {
  1848.         new_slen[0] = (scalefac_comp >> 4) / 5 ;
  1849.         new_slen[1] = (scalefac_comp >> 4) % 5 ;
  1850.         new_slen[2] = (scalefac_comp % 16) >> 2 ;
  1851.         new_slen[3] = (scalefac_comp % 4);
  1852.                 si->ch[ch].gr[gr].preflag = 0;
  1853.  
  1854.                 blocknumber = 0;
  1855.          }
  1856.  
  1857.     else if( scalefac_comp  < 500)
  1858.         {
  1859.         new_slen[0] = ((scalefac_comp - 400 )  >> 2) / 5 ;
  1860.         new_slen[1] = ((scalefac_comp - 400) >> 2) % 5 ;
  1861.         new_slen[2] = (scalefac_comp - 400 ) % 4 ;
  1862.         new_slen[3] = 0;
  1863.                 si->ch[ch].gr[gr].preflag = 0;
  1864.                 blocknumber = 1;
  1865.  
  1866.         }
  1867.  
  1868.     else if( scalefac_comp  < 512)
  1869.         {
  1870.         new_slen[0] = (scalefac_comp - 500 ) / 3 ;
  1871.         new_slen[1] = (scalefac_comp - 500)  % 3 ;
  1872.         new_slen[2] = 0 ;
  1873.         new_slen[3] = 0;
  1874.                 si->ch[ch].gr[gr].preflag = 1;
  1875.                 blocknumber = 2;
  1876.  
  1877.         }
  1878.      }
  1879.  
  1880.     if((((hdr->mode_ext == 1) || (hdr->mode_ext == 3)) && (ch == 1)))
  1881.     {
  1882.       /*   intensity_scale = scalefac_comp %2; */
  1883.          int_scalefac_comp = scalefac_comp >> 1;
  1884.  
  1885.         if(int_scalefac_comp  < 180)
  1886.         {
  1887.         new_slen[0] = int_scalefac_comp  / 36 ;
  1888.         new_slen[1] = (int_scalefac_comp % 36 ) / 6 ;
  1889.         new_slen[2] = (int_scalefac_comp % 36) % 6;
  1890.         new_slen[3] = 0;
  1891.                 si->ch[ch].gr[gr].preflag = 0;
  1892.                 blocknumber = 3;
  1893.  
  1894.          }
  1895.  
  1896.     else if( int_scalefac_comp  < 244)
  1897.         {
  1898.         new_slen[0] = ((int_scalefac_comp - 180 )  % 64 ) >> 4 ;
  1899.         new_slen[1] = ((int_scalefac_comp - 180) % 16) >> 2 ;
  1900.         new_slen[2] = (int_scalefac_comp - 180 ) % 4 ;
  1901.         new_slen[3] = 0;
  1902.                 si->ch[ch].gr[gr].preflag = 0;
  1903.                 blocknumber = 4;
  1904.  
  1905.         }
  1906.  
  1907.     else if( int_scalefac_comp  < 255)
  1908.         {
  1909.         new_slen[0] = (int_scalefac_comp - 244 ) / 3 ;
  1910.         new_slen[1] = (int_scalefac_comp - 244 )  % 3 ;
  1911.         new_slen[2] = 0 ;
  1912.         new_slen[3] = 0;
  1913.                 si->ch[ch].gr[gr].preflag = 0;
  1914.                 blocknumber = 5;
  1915.  
  1916.         }
  1917.      }
  1918.  
  1919.      for (i=0;i< 45;i++) scalefac_buffer[i] = 0;
  1920.  
  1921.      k = 0;
  1922.      for (i = 0;i < 4;i++)
  1923.      {
  1924.           for(j = 0; j < nr_of_sfb_block[blocknumber][blocktypenumber][i]; j++)
  1925.         {
  1926.            if(new_slen[i] == 0)
  1927.            {
  1928.             scalefac_buffer[k] = 0;
  1929.            }
  1930.            else
  1931.            {
  1932.                 scalefac_buffer[k] =  hgetbits(new_slen[i]);
  1933.            }
  1934.            k++;
  1935.  
  1936.         }
  1937.      }
  1938.  
  1939. }
  1940.  
  1941.  
  1942. void III_get_LSF_scale_factors(
  1943.     III_scalefac_t    *scalefac,
  1944.     III_side_info_t    *si,
  1945.     int             gr,
  1946.     int             ch,
  1947.     frame_params    *fr_ps)
  1948. {
  1949.     int             sfb, /*i,*/k = 0, window;
  1950.     struct gr_info_s    *gr_info = &(si->ch[ch].gr[gr]);
  1951.  
  1952.     III_get_LSF_scale_data(scalefac, si, gr, ch, fr_ps);
  1953.  
  1954.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  1955.  
  1956.     if (gr_info->mixed_block_flag) {    /* MIXED */ /* NEW - ag 11/25 */
  1957.         for (sfb = 0; sfb < 8; sfb++) {
  1958.         (*scalefac)[ch].l[sfb] = scalefac_buffer[k];
  1959.         k++;
  1960.         }
  1961.         for (sfb = 3; sfb < 12; sfb++)
  1962.         for (window=0; window<3; window++) {
  1963.             (*scalefac)[ch].s[window][sfb] = scalefac_buffer[k];
  1964.             k++;
  1965.         }
  1966.         for (sfb=12,window=0; window<3; window++)
  1967.         (*scalefac)[ch].s[window][sfb] = 0;
  1968.  
  1969.     } else {                /* SHORT*/
  1970.  
  1971.         for (sfb = 0; sfb < 12; sfb++)
  1972.         for (window=0; window<3; window++) {
  1973.             (*scalefac)[ch].s[window][sfb] = scalefac_buffer[k];
  1974.             k++;
  1975.         }
  1976.         for (sfb=12,window=0; window<3; window++)
  1977.         (*scalefac)[ch].s[window][sfb] = 0;
  1978.     }
  1979.  
  1980.     } else {                    /* LONG types 0,1,3 */
  1981.  
  1982.     for (sfb = 0; sfb < 21; sfb++) {
  1983.         (*scalefac)[ch].l[sfb] = scalefac_buffer[k];
  1984.         k++;
  1985.     }
  1986.     (*scalefac)[ch].l[22] = 0;
  1987.     }
  1988. }
  1989.  
  1990.  
  1991. #ifndef BUILTIN_TABLES
  1992.  
  1993. void initialize_huffman() {
  1994.     FILE    *fi;
  1995.     static int     huffman_initialized = FALSE;
  1996.  
  1997.     if (huffman_initialized)
  1998.     return;
  1999.     if (!(fi = OpenTableFile("huffdec") )) {
  2000.     fprintf(stderr, "Please check huffman table 'huffdec'\n");
  2001.     exit(1);
  2002.     }
  2003.  
  2004.     if (fi == NULL) {
  2005.     fprintf(stderr,"decoder table open error\n");
  2006.     exit(3);
  2007.     }
  2008.  
  2009.     if (read_decoder_table(fi) != HTN) {
  2010.     fprintf(stderr,"decoder table read error\n");
  2011.     exit(4);
  2012.     }
  2013.     fclose(fi);
  2014.     huffman_initialized = TRUE;
  2015. }
  2016. #endif
  2017.  
  2018.  
  2019. /*#define OPTIMIZETHIS*/
  2020.  
  2021. void III_hufman_decode(
  2022.     long int         is[SBLIMIT][SSLIMIT],
  2023.     III_side_info_t    *si,
  2024.     int             ch,
  2025.     int             gr,
  2026.     int             part2_start,
  2027.     frame_params    *fr_ps)
  2028. {
  2029.     int             i, x, y;
  2030.     int             v, w;
  2031.     struct huffcodetab    *h;
  2032.     int             region1Start;
  2033.     int             region2Start;
  2034.     int             sfreq;
  2035.     int             currentBit, grBits;
  2036.     my_gr_info        *gi;
  2037. #ifdef OPTIMIZETHIS
  2038.     long int        *isptr;
  2039. #endif
  2040.     /*int         bt = (*si).ch[ch].gr[gr].window_switching_flag &&
  2041.                  ((*si).ch[ch].gr[gr].block_type == 2);*/
  2042.  
  2043.     gi = (my_gr_info *) &(*si).ch[ch].gr[gr];
  2044.     sfreq = fr_ps->header->sampling_frequency + (fr_ps->header->version * 3);
  2045.     initialize_huffman();
  2046.  
  2047.     /* Find region boundary for short block case */
  2048.  
  2049.     if ( ((*si).ch[ch].gr[gr].window_switching_flag) &&
  2050.     ((*si).ch[ch].gr[gr].block_type == 2) ) {
  2051.  
  2052.     /* Region2 */
  2053.     region1Start = 36;    /* sfb[9/3]*3=36 */
  2054.     region2Start = 576;    /* No Region2 for short block case */
  2055.  
  2056.     } else {       /* Find region boundary for long block case */
  2057.  
  2058.     region1Start = sfBandIndex[sfreq]
  2059.             .l[(*si).ch[ch].gr[gr].region0_count + 1]; /* MI */
  2060.     region2Start = sfBandIndex[sfreq]
  2061.             .l[(*si).ch[ch].gr[gr].region0_count +
  2062.             (*si).ch[ch].gr[gr].region1_count + 2]; /* MI */
  2063.     }
  2064.  
  2065.     grBits     = part2_start + (*si).ch[ch].gr[gr].part2_3_length;
  2066.     currentBit = hsstell();
  2067.  
  2068.     /* Read bigvalues area */
  2069.     for (i = 0; i < (*si).ch[ch].gr[gr].big_values * 2; i += 2) {
  2070.     if (i < region1Start)
  2071.         h = &ht[(*si).ch[ch].gr[gr].table_select[0]];
  2072.     else if (i<region2Start)
  2073.         h = &ht[(*si).ch[ch].gr[gr].table_select[1]];
  2074.     else
  2075.         h = &ht[(*si).ch[ch].gr[gr].table_select[2]];
  2076.     huffman_decoder(h, &x, &y, &v, &w);
  2077.     is[i / SSLIMIT][i % SSLIMIT] = x;
  2078.     is[(i+1) / SSLIMIT][(i+1) % SSLIMIT] = y;
  2079.     }
  2080.  
  2081.     grBits     = part2_start + (*si).ch[ch].gr[gr].part2_3_length;
  2082.     currentBit = hsstell();
  2083.  
  2084.     /* Read count1 area */
  2085.     h = &ht[(*si).ch[ch].gr[gr].count1table_select + 32];
  2086. #ifdef OPTIMIZETHIS
  2087.     isptr = &is[i / SSLIMIT][i % SSLIMIT];
  2088.     while ((hsstell() < part2_start + (*si).ch[ch].gr[gr].part2_3_length ) &&
  2089.         (isptr < &is[SBLIMIT-1][SSLIMIT-1]) ) {
  2090.     huffman_decoder(h, (int *)&isptr[2], (int *)&isptr[3],
  2091.                (int *)&isptr[0], (int *)&isptr[1]);
  2092.     isptr += 4;
  2093.     }
  2094. #else
  2095.     while ((hsstell() < part2_start + (*si).ch[ch].gr[gr].part2_3_length ) &&
  2096.         ( i < SSLIMIT*SBLIMIT )) {
  2097.     huffman_decoder(h, &x, &y, &v, &w);
  2098.     is[i / SSLIMIT][i % SSLIMIT] = v;
  2099.     is[(i+1) / SSLIMIT][(i+1) % SSLIMIT] = w;
  2100.     is[(i+2) / SSLIMIT][(i+2) % SSLIMIT] = x;
  2101.     is[(i+3) / SSLIMIT][(i+3) % SSLIMIT] = y;
  2102.     i += 4;
  2103.     }
  2104. #endif
  2105.  
  2106.     grBits     = part2_start + (*si).ch[ch].gr[gr].part2_3_length;
  2107.     currentBit = hsstell();
  2108.  
  2109.     if (hsstell() > part2_start + (*si).ch[ch].gr[gr].part2_3_length) {
  2110.     i -=4;
  2111.     rewindNbits(hsstell()-part2_start - (*si).ch[ch].gr[gr].part2_3_length);
  2112.     }
  2113.  
  2114.     /* Dismiss stuffing Bits */
  2115.     grBits     = part2_start + (*si).ch[ch].gr[gr].part2_3_length;
  2116.     currentBit = hsstell();
  2117.     if ( currentBit < grBits )
  2118.     hgetbits( grBits - currentBit );
  2119.  
  2120.     /* Zero out rest */
  2121.     for (; i < SSLIMIT * SBLIMIT; i++)
  2122.     is[i / SSLIMIT][i % SSLIMIT] = 0;
  2123. }
  2124.  
  2125.  
  2126. static int pretab[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
  2127.  
  2128. void III_dequantize_sample(
  2129.     long int         is[SBLIMIT][SSLIMIT],
  2130.     REAL         xr[SBLIMIT][SSLIMIT],
  2131.     III_scalefac_t    *scalefac,
  2132.     struct gr_info_s    *gr_info,
  2133.     int             ch,
  2134.     frame_params    *fr_ps)
  2135. {
  2136.     int            ss, sb, cb = 0, sfreq;
  2137.     int            next_cb_boundary, cb_begin = 0, cb_width = 0;
  2138. #if 0
  2139. /*#ifdef OPTIMIZE*/
  2140.     static REAL        globalGain;
  2141.     static int        lastGlobalGain = -1;
  2142.  
  2143. /* A pow(2, n) cache, where n = [-16..5] step 0.25 */
  2144. #define POW2CACHENEG (16*4)        /* # of cached numbers for n < 0   */
  2145. #define POW2CACHEPOS (5*4)        /* # of cached numbers for n > 0   */
  2146. static REAL pow2[POW2CACHENEG+POW2CACHEPOS+1] = { /* n < 0 + n > 0 + n = 0 */
  2147. /* <<n>>    <<pow(2,n)>> */
  2148. /*-16.00*/  0.00001526,  0.00001815,  0.00002158,  0.00002566,  0.00003052,
  2149. /*-14.75*/  0.00003629,  0.00004316,  0.00005132,  0.00006104,  0.00007258,
  2150. /*-13.50*/  0.00008632,  0.00010265,  0.00012207,  0.00014517,  0.00017263,
  2151. /*-12.25*/  0.00020530,  0.00024414,  0.00029033,  0.00034527,  0.00041059,
  2152. /*-11.00*/  0.00048828,  0.00058067,  0.00069053,  0.00082119,  0.00097656,
  2153. /* -9.75*/  0.00116134,  0.00138107,  0.00164238,  0.00195312,  0.00232267,
  2154. /* -8.50*/  0.00276214,  0.00328475,  0.00390625,  0.00464534,  0.00552427,
  2155. /* -7.25*/  0.00656950,  0.00781250,  0.00929068,  0.01104854,  0.01313901,
  2156. /* -6.00*/  0.01562500,  0.01858136,  0.02209709,  0.02627801,  0.03125000,
  2157. /* -4.75*/  0.03716272,  0.04419417,  0.05255603,  0.06250000,  0.07432544,
  2158. /* -3.50*/  0.08838835,  0.10511205,  0.12500000,  0.14865089,  0.17677670,
  2159. /* -2.25*/  0.21022410,  0.25000000,  0.29730178,  0.35355339,  0.42044821,
  2160. /* -1.00*/  0.50000000,  0.59460356,  0.70710678,  0.84089642,  1.00000000,
  2161. /*  0.25*/  1.18920712,  1.41421356,  1.68179283,  2.00000000,  2.37841423,
  2162. /*  1.50*/  2.82842712,  3.36358566,  4.00000000,  4.75682846,  5.65685425,
  2163. /*  2.75*/  6.72717132,  8.00000000,  9.51365692, 11.31370850, 13.45434264,
  2164. /*  4.00*/ 16.00000000, 19.02731384, 22.62741700, 26.90868529, 32.00000000
  2165. };
  2166.  
  2167.     /* Skip calculation unless value has changed */
  2168.     if (gr_info->global_gain != lastGlobalGain) {
  2169.  
  2170.     /* Compute overall (global) scaling */
  2171.  
  2172.     /* All vars are integers -> required accuracy = 0.25 */
  2173.     int n = gr_info->global_gain - 210;
  2174.     if (n >= -POW2CACHENEG && n <= POW2CACHEPOS)
  2175.         globalGain = pow2[n + POW2CACHENEG];      /* pow2[60] = 2^0 = 1 */
  2176.     else {
  2177.         globalGain = pow(2.0, (float)n * 0.25);
  2178.         /*fprintf(stderr, "pow(2,n)[1] miss, n=%4.2f\n", (float)n * 0.25);*/
  2179.     }
  2180.     /* Original calculation */
  2181.     /* globalGain = pow(2.0, 0.25 * (gr_info->global_gain - 210.0)); */
  2182.  
  2183.     lastGlobalGain = gr_info->global_gain;
  2184.     }
  2185.  
  2186.     sfreq = fr_ps->header->sampling_frequency + (fr_ps->header->version * 3);
  2187.  
  2188.     /* Choose correct scalefactor band per block type, initalize boundary */
  2189.  
  2190.     if (gr_info->window_switching_flag && (gr_info->block_type == 2))
  2191.     if (gr_info->mixed_block_flag)
  2192.         next_cb_boundary=sfBandIndex[sfreq].l[1];    /* LONG blocks: 0,1,3 */
  2193.     else {
  2194.         next_cb_boundary=sfBandIndex[sfreq].s[1]*3;    /* Pure SHORT block */
  2195.         cb_width = sfBandIndex[sfreq].s[1];
  2196.         cb_begin = 0;
  2197.     } else
  2198.         next_cb_boundary=sfBandIndex[sfreq].l[1];    /* LONG blocks: 0,1,3 */
  2199.  
  2200.     /* Apply formula per block type */
  2201.  
  2202.     for (sb = 0 ; sb < SBLIMIT ; sb++) {    /* 32 */
  2203.     for (ss = 0 ; ss < SSLIMIT ; ss++) {    /* 18 */
  2204.  
  2205.         int        insample;
  2206.         REAL    outsample;
  2207.  
  2208.         if ((sb * 18) + ss == next_cb_boundary) {
  2209.         /* Adjust critical band boundary */
  2210.  
  2211.         if (gr_info->window_switching_flag &&
  2212.                     (gr_info->block_type == 2)) {
  2213.             if (gr_info->mixed_block_flag)  {
  2214.             if (((sb * 18) + ss) == sfBandIndex[sfreq].l[8]) {
  2215.                 next_cb_boundary=sfBandIndex[sfreq].s[4] * 3;
  2216.                 cb = 3;
  2217.                 cb_width = sfBandIndex[sfreq].s[cb+1] -
  2218.                     sfBandIndex[sfreq].s[cb];
  2219.                 cb_begin = sfBandIndex[sfreq].s[cb] * 3;
  2220.             } else if (((sb * 18) + ss) < sfBandIndex[sfreq].l[8])
  2221.                 next_cb_boundary = sfBandIndex[sfreq].l[(++cb) + 1];
  2222.             else {
  2223.                 next_cb_boundary =
  2224.                     sfBandIndex[sfreq].s[(++cb) + 1] * 3;
  2225.                 cb_width = sfBandIndex[sfreq].s[cb + 1] -
  2226.                     sfBandIndex[sfreq].s[cb];
  2227.                 cb_begin = sfBandIndex[sfreq].s[cb] * 3;
  2228.             }
  2229.             } else {
  2230.             next_cb_boundary = sfBandIndex[sfreq].s[(++cb) + 1] * 3;
  2231.             cb_width = sfBandIndex[sfreq].s[cb+1] -
  2232.                     sfBandIndex[sfreq].s[cb];
  2233.             cb_begin = sfBandIndex[sfreq].s[cb]*3;
  2234.             }
  2235.  
  2236.         } else /* long blocks */
  2237.  
  2238.             next_cb_boundary = sfBandIndex[sfreq].l[(++cb) + 1];
  2239.         }
  2240.  
  2241.         /* Do long/short dependent scaling operations */
  2242.  
  2243.         /* Initialize to overall (global) scaling */
  2244.         outsample = globalGain;
  2245.  
  2246.         if (gr_info->window_switching_flag && (
  2247.             ((gr_info->block_type == 2) &&
  2248.             (gr_info->mixed_block_flag == 0)) ||
  2249.             ((gr_info->block_type == 2) &&
  2250.             gr_info->mixed_block_flag && (sb >= 2)) )) {
  2251.  
  2252.         /* All vars are integers -> required accuracy = 2 */
  2253.         /* Ending expression = 1 should return pow(2, -2) */
  2254.         int n = 4 * (-2 * gr_info->subblock_gain[(((sb * 18) + ss) -
  2255.                 cb_begin) / cb_width]);
  2256.  
  2257.         if (n >= -POW2CACHENEG && n <= POW2CACHEPOS)
  2258.             outsample *= pow2[n + POW2CACHENEG];
  2259.         else {
  2260.             outsample *= pow(2.0, (float)n * 0.25);
  2261.             /*fprintf(stderr, "pow(2,n)[2] miss, n=%4.2f\n", n*0.25);*/
  2262.         }
  2263.  
  2264.         /* All vars are integers -> required accuracy = 0.5 */
  2265.         /* Ending expression = 1 should return pow(2, -0.5) */
  2266.         n = (int)-2 * ((1.0 + gr_info->scalefac_scale) *
  2267.              (*scalefac)[ch].s[(((sb * 18) + ss) -
  2268.              cb_begin) / cb_width][cb]);
  2269.  
  2270.         if (n >= -POW2CACHENEG && n <= POW2CACHEPOS)
  2271.             outsample *= pow2[n + POW2CACHENEG];
  2272.         else {
  2273.             outsample *= pow(2.0, (float)n * 0.25);
  2274.             /*fprintf(stderr, "pow(2,n)[3] miss, n=%4.2f\n", n*0.25);*/
  2275.         }
  2276.  
  2277.         } else {    /* LONG block types 0,1,3 & */
  2278.             /* 1st 2 subbands of switched blocks */
  2279.  
  2280.         /* All vars are integers -> required accuracy = 0.5 */
  2281.         /* Ending expression = 1 should return pow(2, -0.5) */
  2282.         int n;
  2283.         n = (int)-2 * ((1.0 + gr_info->scalefac_scale) *
  2284.              ((*scalefac)[ch].l[cb] +
  2285.              gr_info->preflag * pretab[cb]));
  2286.  
  2287.         if (n >= -POW2CACHENEG && n <= POW2CACHEPOS)
  2288.             outsample *= pow2[n + POW2CACHENEG];
  2289.         else {
  2290.             if (n > -1000) {
  2291.             outsample *= pow(2.0, (float)n * 0.5);
  2292.             /* fprintf(stderr, "pow(2,n)[4] miss, n=%4.2f\n", */
  2293.             /*    (float)n  -0.5); */
  2294.             } else
  2295.             /* Bug in the implementation here? x sometimes */
  2296.             /* underflows like -2009044012. Then mute. */
  2297.             outsample = 0.0;
  2298.         }
  2299.         }
  2300.  
  2301.         /* Scale quantized value */
  2302.         insample = is[sb][ss];        /* read input array once */
  2303.         if (insample == 0)
  2304.         outsample = 0.0;        /* optimize no input */
  2305.         else {
  2306.  
  2307. /* A pow(n, 4/3) cache, where n = [1..100] step 1 */
  2308. #define POWCACHED 100    /* biggest number that is cached */
  2309. static REAL powc[POWCACHED] = {
  2310. /* 1.00*/   1.00000000,   2.51984210,   4.32674871,   6.34960421,   8.54987973,
  2311. /* 6.00*/  10.90272356,  13.39051828,  16.00000000,  18.72075441,  21.54434690,
  2312. /*11.00*/  24.46378100,  27.47314182,  30.56735094,  33.74199170,  36.99318111,
  2313. /*16.00*/  40.31747360,  43.71178704,  47.17334510,  50.69963133,  54.28835233,
  2314. /*21.00*/  57.93740770,  61.64486527,  65.40894054,  69.22797937,  73.10044346,
  2315. /*26.00*/  77.02489778,  81.00000000,  85.02449121,  89.09718794,  93.21697518,
  2316. /*31.00*/  97.38280022, 101.59366733, 105.84863289, 110.14680124, 114.48732086,
  2317. /*36.00*/ 118.86938096, 123.29220851, 127.75506546, 132.25724628, 136.79807573,
  2318. /*41.00*/ 141.37690686, 145.99311909, 150.64611660, 155.33532675, 160.06019870,
  2319. /*46.00*/ 164.82020207, 169.61482577, 174.44357691, 179.30597979, 184.20157493,
  2320. /*51.00*/ 189.12991823, 194.09058015, 199.08314497, 204.10721008, 209.16238534,
  2321. /*56.00*/ 214.24829247, 219.36456448, 224.51084516, 229.68678854, 234.89205847,
  2322. /*61.00*/ 240.12632817, 245.38927980, 250.68060410, 256.00000000, 261.34717431,
  2323. /*66.00*/ 266.72184136, 272.12372273, 277.55254693, 283.00804915, 288.48997099,
  2324. /*71.00*/ 293.99806021, 299.53207052, 305.09176134, 310.67689758, 316.28724949,
  2325. /*76.00*/ 321.92259240, 327.58270661, 333.26737717, 338.97639374, 344.70955041,
  2326. /*81.00*/ 350.46664558, 356.24748183, 362.05186573, 367.87960775, 373.73052213,
  2327. /*86.00*/ 379.60442677, 385.50114309, 391.42049594, 397.36231351, 403.32642719,
  2328. /*91.00*/ 409.31267152, 415.32088406, 421.35090534, 427.40257871, 433.47575036,
  2329. /*96.00*/ 439.57026914, 445.68598654, 451.82275662, 457.98043591, 464.15888336
  2330. };
  2331.         int absSample = abs(insample);
  2332.  
  2333.         if (absSample <= POWCACHED)
  2334.             outsample *= powc[absSample - 1];
  2335.         else
  2336.             outsample *= pow(absSample, 4.0/3.0);
  2337.         if (insample < 0)
  2338.             outsample = -outsample;    /* change the sign */
  2339.         }
  2340.         xr[sb][ss] = outsample;        /* touch output array once */
  2341.     }
  2342.     }
  2343.  
  2344. #else
  2345.     /* Original code */
  2346.  
  2347.     int        sign;
  2348.  
  2349.     sfreq = fr_ps->header->sampling_frequency + (fr_ps->header->version * 3);
  2350.  
  2351.     /* Choose correct scalefactor band per block type, initalize boundary */
  2352.  
  2353.     if (gr_info->window_switching_flag && (gr_info->block_type == 2))
  2354.     if (gr_info->mixed_block_flag)
  2355.         next_cb_boundary=sfBandIndex[sfreq].l[1];    /* LONG blocks: 0,1,3 */
  2356.     else {
  2357.         next_cb_boundary=sfBandIndex[sfreq].s[1]*3;    /* Pure SHORT block */
  2358.         cb_width = sfBandIndex[sfreq].s[1];
  2359.         cb_begin = 0;
  2360.     } else
  2361.         next_cb_boundary=sfBandIndex[sfreq].l[1];    /* LONG blocks: 0,1,3 */
  2362.  
  2363.     /* Apply formula per block type */
  2364.  
  2365.     for (sb = 0 ; sb < SBLIMIT ; sb++) {
  2366.     for (ss = 0 ; ss < SSLIMIT ; ss++) {
  2367.         if ((sb*18) + ss == next_cb_boundary) {
  2368.         /* Adjust critical band boundary */
  2369.  
  2370.         if (gr_info->window_switching_flag &&
  2371.                     (gr_info->block_type == 2)) {
  2372.             if (gr_info->mixed_block_flag)  {
  2373.             if (((sb*18)+ss) == sfBandIndex[sfreq].l[8]) {
  2374.                 next_cb_boundary=sfBandIndex[sfreq].s[4]*3;
  2375.                 cb = 3;
  2376.                 cb_width = sfBandIndex[sfreq].s[cb+1] -
  2377.                     sfBandIndex[sfreq].s[cb];
  2378.                 cb_begin = sfBandIndex[sfreq].s[cb]*3;
  2379.             } else if (((sb*18)+ss) < sfBandIndex[sfreq].l[8])
  2380.                 next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];
  2381.             else {
  2382.                 next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1]*3;
  2383.                 cb_width = sfBandIndex[sfreq].s[cb+1] -
  2384.                     sfBandIndex[sfreq].s[cb];
  2385.                 cb_begin = sfBandIndex[sfreq].s[cb]*3;
  2386.             }
  2387.             } else {
  2388.             next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1]*3;
  2389.             cb_width = sfBandIndex[sfreq].s[cb+1] -
  2390.                     sfBandIndex[sfreq].s[cb];
  2391.             cb_begin = sfBandIndex[sfreq].s[cb]*3;
  2392.             }
  2393.  
  2394.         } else /* long blocks */
  2395.  
  2396.             next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];
  2397.         }
  2398.  
  2399.         /* Compute overall (global) scaling */
  2400.  
  2401.         xr[sb][ss] = pow(2.0, (0.25 * (gr_info->global_gain - 210.0)));
  2402.  
  2403.         /* Do long/short dependent scaling operations */
  2404.  
  2405.         if (gr_info->window_switching_flag && (
  2406.             ((gr_info->block_type == 2) &&
  2407.             (gr_info->mixed_block_flag == 0)) ||
  2408.             ((gr_info->block_type == 2) &&
  2409.             gr_info->mixed_block_flag && (sb >= 2)) )) {
  2410.  
  2411.         xr[sb][ss] *= pow(2.0, 0.25 * -8.0 * gr_info->subblock_gain[
  2412.                 (((sb*18)+ss) - cb_begin)/cb_width]);
  2413.  
  2414.         xr[sb][ss] *= pow(2.0, 0.25 * -2.0 *
  2415.             (1.0+gr_info->scalefac_scale) *
  2416.             (*scalefac)[ch].s[(((sb*18)+ss) -
  2417.             cb_begin)/cb_width][cb]);
  2418.  
  2419.         } else {    /* LONG block types 0,1,3 & */
  2420.             /* 1st 2 subbands of switched blocks */
  2421.  
  2422.         xr[sb][ss] *= pow(2.0, -0.5 * (1.0+gr_info->scalefac_scale)
  2423.                     * ((*scalefac)[ch].l[cb]
  2424.                     + gr_info->preflag * pretab[cb]));
  2425.         }
  2426.  
  2427.         /* Scale quantized value */
  2428.         sign = (is[sb][ss]<0) ? 1 : 0;
  2429.         xr[sb][ss] *= pow(abs(is[sb][ss]), 4.0/3.0);
  2430.         if (sign)
  2431.         xr[sb][ss] = -xr[sb][ss];
  2432.     }
  2433.     }
  2434. #endif /* OPTIMIZE */
  2435. }
  2436.  
  2437.  
  2438. void III_reorder (
  2439.     REAL         xr[SBLIMIT][SSLIMIT],
  2440.     REAL         ro[SBLIMIT][SSLIMIT],
  2441.     struct gr_info_s    *gr_info,
  2442.     frame_params    *fr_ps)
  2443. {
  2444.     int        sfreq;
  2445.     int        sfb, sfb_start, sfb_lines;
  2446.     int        sb, ss, window, freq, src_line, des_line;
  2447.  
  2448.     sfreq=fr_ps->header->sampling_frequency + (fr_ps->header->version * 3);
  2449.  
  2450. #ifdef OPTIMIZE
  2451.  
  2452.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  2453.  
  2454.     /* Clear the whole array */
  2455.     memset((void *)&ro[0][0], 0, sizeof(REAL)*SBLIMIT*SSLIMIT);
  2456.  
  2457.     if (gr_info->mixed_block_flag) {
  2458.         /* No reorder for low 2 subbands */
  2459.         for (sb=0 ; sb < 2 ; sb++)
  2460.         for (ss=0 ; ss < SSLIMIT ; ss++) {
  2461.             ro[sb][ss] = xr[sb][ss];
  2462.         }
  2463.         /* Reordering for rest switched short */
  2464.         for (sfb = 3, sfb_start = sfBandIndex[sfreq].s[3],
  2465.             sfb_lines=sfBandIndex[sfreq].s[4] - sfb_start;
  2466.             sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  2467.             (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  2468.         for(window=0; window<3; window++)
  2469.             for(freq=0;freq<sfb_lines;freq++) {
  2470.             src_line = sfb_start*3 + window*sfb_lines + freq;
  2471.             des_line = (sfb_start*3) + window + (freq*3);
  2472.             ro[des_line/SSLIMIT][des_line%SSLIMIT] =
  2473.                 xr[src_line/SSLIMIT][src_line%SSLIMIT];
  2474.             }
  2475.  
  2476.     } else {    /* pure short */
  2477.  
  2478.         for (sfb=0,sfb_start=0,sfb_lines=sfBandIndex[sfreq].s[1];
  2479.             sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  2480.             (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  2481.         for (window=0; window<3; window++)
  2482.             for(freq=0;freq<sfb_lines;freq++) {
  2483.             src_line = sfb_start*3 + window*sfb_lines + freq;
  2484.             des_line = (sfb_start*3) + window + (freq*3);
  2485.             ro[des_line/SSLIMIT][des_line%SSLIMIT] =
  2486.                 xr[src_line/SSLIMIT][src_line%SSLIMIT];
  2487.             }
  2488.     }
  2489.  
  2490.     } else {    /* long blocks */
  2491.  
  2492.     /* Copy array xr[] to ro[] */
  2493.     memcpy((void *)&ro[0][0], (void *)&xr[0][0],
  2494.         sizeof(REAL)*SBLIMIT*SSLIMIT);
  2495.     }
  2496.  
  2497. #else
  2498.     /* Original code */
  2499.  
  2500.     for (sb=0; sb<SBLIMIT; sb++)
  2501.     for (ss=0; ss<SSLIMIT; ss++)
  2502.         ro[sb][ss] = 0.0;
  2503.  
  2504.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  2505.     if (gr_info->mixed_block_flag) {
  2506.         /* No reorder for low 2 subbands */
  2507.         for (sb=0 ; sb < 2 ; sb++)
  2508.         for (ss=0 ; ss < SSLIMIT ; ss++) {
  2509.             ro[sb][ss] = xr[sb][ss];
  2510.         }
  2511.         /* Reordering for rest switched short */
  2512.         for (sfb = 3, sfb_start = sfBandIndex[sfreq].s[3],
  2513.             sfb_lines=sfBandIndex[sfreq].s[4] - sfb_start;
  2514.             sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  2515.             (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  2516.         for(window=0; window<3; window++)
  2517.             for(freq=0;freq<sfb_lines;freq++) {
  2518.             src_line = sfb_start*3 + window*sfb_lines + freq;
  2519.             des_line = (sfb_start*3) + window + (freq*3);
  2520.             ro[des_line/SSLIMIT][des_line%SSLIMIT] =
  2521.                 xr[src_line/SSLIMIT][src_line%SSLIMIT];
  2522.             }
  2523.  
  2524.     } else {    /* pure short */
  2525.  
  2526.         for (sfb=0,sfb_start=0,sfb_lines=sfBandIndex[sfreq].s[1];
  2527.             sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  2528.             (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  2529.         for (window=0; window<3; window++)
  2530.             for(freq=0;freq<sfb_lines;freq++) {
  2531.             src_line = sfb_start*3 + window*sfb_lines + freq;
  2532.             des_line = (sfb_start*3) + window + (freq*3);
  2533.             ro[des_line/SSLIMIT][des_line%SSLIMIT] =
  2534.                 xr[src_line/SSLIMIT][src_line%SSLIMIT];
  2535.             }
  2536.     }
  2537.  
  2538.     } else {    /* long blocks */
  2539.  
  2540.     for (sb=0 ; sb < SBLIMIT ; sb++)
  2541.         for (ss=0 ; ss < SSLIMIT ; ss++)
  2542.         ro[sb][ss] = xr[sb][ss];
  2543.     }
  2544. #endif /* OPTIMIZE */
  2545. }
  2546.  
  2547.  
  2548. static void III_i_stereo_k_values(
  2549.     int        is_pos,
  2550.     REAL    io,
  2551.     int        i,
  2552.     REAL FAR    k[2][576])
  2553. {
  2554. #ifdef OPTIMIZE
  2555.     if (is_pos == 0) {
  2556.     k[0][i] = k[1][i] = 1.0;
  2557.     } else if (is_pos & 1) {
  2558.     k[1][i] = 1.0;
  2559.     if (is_pos == 1)
  2560.         k[0][i] = io;        /* pow(n,1) */
  2561.     else if (is_pos == 3)
  2562.         k[0][i] = io * io;        /* pow(n,2) */
  2563.     else if (is_pos == 5)
  2564.         k[0][i] = io * io * io;    /* pow(n,3) */
  2565.     else {
  2566.         k[0][i] = pow(io, (float)(is_pos + 1)/2.0);
  2567.         /*printf("pow(%4.2f, %4.2f) ", io, (float)(is_pos + 1)/2.0);
  2568.         fflush(stdout);*/
  2569.     }
  2570.     } else {
  2571.     k[0][i] = 1.0;
  2572.     if (is_pos == 2)
  2573.         k[1][i] = io;        /* pow(n,1) */
  2574.     else if (is_pos == 4)
  2575.         k[1][i] = io * io;        /* pow(n,2) */
  2576.     else if (is_pos == 6)
  2577.         k[1][i] = io * io * io;    /* pow(n,3) */
  2578.     else {
  2579.         k[1][i] = pow(io, (float)is_pos/2.0);
  2580.         /*printf("pow(%4.2f, %4.2f) ", io, (float)is_pos/2.0);
  2581.         fflush(stdout);*/
  2582.     }
  2583.     }
  2584.  
  2585. #else /* the orginal code */
  2586.  
  2587.     if (is_pos == 0) {
  2588.     k[0][i] = 1.0;
  2589.     k[1][i] = 1.0;
  2590.     } else if ((is_pos % 2) == 1) {
  2591.     k[0][i] = pow(io, (float)(is_pos + 1)/2.0);
  2592.     k[1][i] = 1.0;
  2593.     } else {
  2594.     k[0][i] = 1.0;
  2595.     k[1][i] = pow(io, (float)is_pos/2.0);
  2596.     }
  2597. #endif /* OPTIMIZE */
  2598. }
  2599.  
  2600.  
  2601. void III_stereo(
  2602.     REAL         xr[2][SBLIMIT][SSLIMIT],
  2603.     REAL         lr[2][SBLIMIT][SSLIMIT],
  2604.     III_scalefac_t    *scalefac,
  2605.     struct gr_info_s    *gr_info,
  2606.     frame_params    *fr_ps)
  2607. {
  2608.     int            sfreq;
  2609.     int            stereo = fr_ps->stereo;
  2610.     int            ms_stereo =
  2611.                 (fr_ps->header->mode == MPG_MD_JOINT_STEREO) &&
  2612.                 (fr_ps->header->mode_ext & 0x2);
  2613.     int            i_stereo =
  2614.                 (fr_ps->header->mode == MPG_MD_JOINT_STEREO) &&
  2615.                 (fr_ps->header->mode_ext & 0x1);
  2616.     int            sfb;
  2617.     int            i, j, sb, ss, ch;
  2618.     REAL        io;
  2619. #ifdef OPTIMIZE
  2620.     static short    is_pos[SBLIMIT*SSLIMIT];
  2621.     static REAL        is_ratio[SBLIMIT*SSLIMIT];
  2622.     static REAL FAR    k[2][SBLIMIT*SSLIMIT];
  2623.  
  2624. #define QUICKTAN(dest, src) \
  2625.     if (((src)) == 1) \
  2626.         (dest) = 0.267949192431; \
  2627.     else if (((src)) == 2) \
  2628.         (dest) = 0.577350269189; \
  2629.     else if (((src)) == 3) \
  2630.         (dest) = 1.0; \
  2631.     else if (((src)) == 4) \
  2632.         (dest) = 1.73205080758; \
  2633.     else { \
  2634.         (dest)= tan((float)(src) * (PI / 12)); \
  2635.         /*printf("tan(%d) ", (src)); fflush(stdout);*/ \
  2636.     }
  2637. #else
  2638.     short        is_pos[SBLIMIT*SSLIMIT];
  2639.     REAL        is_ratio[SBLIMIT*SSLIMIT];
  2640.     REAL FAR        k[2][SBLIMIT*SSLIMIT];
  2641. #endif
  2642.  
  2643.     int lsf = (fr_ps->header->version == MPEG_PHASE2_LSF);
  2644.  
  2645.     if ((gr_info->scalefac_compress % 2) == 1)
  2646.     io = 0.707106781188;
  2647.     else
  2648.     io = 0.840896415256;
  2649.  
  2650.     sfreq = fr_ps->header->sampling_frequency + (fr_ps->header->version * 3);
  2651.  
  2652.     /* Intialization */
  2653.     for (i = 0; i < SBLIMIT*SSLIMIT; i++ )
  2654.     is_pos[i] = 7;
  2655.  
  2656.     if ((stereo == 2) && i_stereo ) {
  2657.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  2658.         if( gr_info->mixed_block_flag ) {
  2659.         int max_sfb = 0;
  2660.  
  2661.         for ( j=0; j<3; j++ ) {
  2662.             int sfbcnt;
  2663.             sfbcnt = 2;
  2664.             for (sfb=12; sfb >=3; sfb--) {
  2665.             int lines;
  2666.             lines = sfBandIndex[sfreq].s[sfb+1] -
  2667.                 sfBandIndex[sfreq].s[sfb];
  2668.             i = 3*sfBandIndex[sfreq].s[sfb] + (j+1) * lines - 1;
  2669.             while (lines > 0) {
  2670.                 if (xr[1][i/SSLIMIT][i%SSLIMIT] != 0.0) {
  2671.                 sfbcnt = sfb;
  2672.                 sfb = -10;
  2673.                 lines = -10;
  2674.                 }
  2675.                 lines--;
  2676.                 i--;
  2677.             }
  2678.             }
  2679.             sfb = sfbcnt + 1;
  2680.  
  2681.             if ( sfb > max_sfb )
  2682.             max_sfb = sfb;
  2683.  
  2684.             while (sfb<12) {
  2685.             sb = sfBandIndex[sfreq].s[sfb+1] -
  2686.                 sfBandIndex[sfreq].s[sfb];
  2687.             i = 3*sfBandIndex[sfreq].s[sfb] + j * sb;
  2688.             for ( ; sb > 0; sb--) {
  2689.                 is_pos[i] = (*scalefac)[1].s[j][sfb];
  2690.                 if ( is_pos[i] != 7 )
  2691.                 if (lsf) {
  2692.                     III_i_stereo_k_values(is_pos[i],io,i,k);
  2693.                 } else {
  2694. #ifdef OPTIMIZE
  2695.                     QUICKTAN(is_ratio[i], is_pos[i])
  2696. #else
  2697.                     is_ratio[i] = tan((float)is_pos[i] *
  2698.                         (PI / 12));
  2699. #endif
  2700.                 }
  2701.                 i++;
  2702.             }
  2703.             sfb++;
  2704.             }
  2705.  
  2706.             sb = sfBandIndex[sfreq].s[12]-sfBandIndex[sfreq].s[11];
  2707.             sfb = 3*sfBandIndex[sfreq].s[11] + j * sb;
  2708.             sb = sfBandIndex[sfreq].s[13]-sfBandIndex[sfreq].s[12];
  2709.  
  2710.             i = 3*sfBandIndex[sfreq].s[11] + j * sb;
  2711.             for ( ; sb > 0; sb-- ) {
  2712.             is_pos[i] = is_pos[sfb];
  2713.             is_ratio[i] = is_ratio[sfb];
  2714.             k[0][i] = k[0][sfb];
  2715.             k[1][i] = k[1][sfb];
  2716.             i++;
  2717.             }
  2718.         }
  2719.         if ( max_sfb <= 3 ) {
  2720.             i = 2;
  2721.             ss = 17;
  2722.             sb = -1;
  2723.             while ( i >= 0 ) {
  2724.             if ( xr[1][i][ss] != 0.0 ) {
  2725.                 sb = i*18+ss;
  2726.                 i = -1;
  2727.             } else {
  2728.                 ss--;
  2729.                 if ( ss < 0 ) {
  2730.                 i--;
  2731.                 ss = 17;
  2732.                 }
  2733.             }
  2734.             }
  2735.             i = 0;
  2736.             while ( sfBandIndex[sfreq].l[i] <= sb )
  2737.             i++;
  2738.             sfb = i;
  2739.             i = sfBandIndex[sfreq].l[i];
  2740.             for ( ; sfb<8; sfb++ ) {
  2741.             sb = sfBandIndex[sfreq].l[sfb+1]-sfBandIndex[sfreq].l[sfb];
  2742.             for ( ; sb > 0; sb--) {
  2743.                 is_pos[i] = (*scalefac)[1].l[sfb];
  2744.                 if ( is_pos[i] != 7 )
  2745.                 if ( lsf ) {
  2746.                     III_i_stereo_k_values(is_pos[i],io,i,k);
  2747.                 } else {
  2748. #ifdef OPTIMIZE
  2749.                     QUICKTAN(is_ratio[i], is_pos[i])
  2750. #else
  2751.                     is_ratio[i] = tan((float)is_pos[i] *
  2752.                         (PI / 12));
  2753. #endif
  2754.                 }
  2755.                 i++;
  2756.             }
  2757.             }
  2758.         }
  2759.  
  2760.         } else {
  2761.  
  2762.         for ( j=0; j<3; j++ ) {
  2763.             int sfbcnt;
  2764.             sfbcnt = -1;
  2765.             for( sfb=12; sfb >=0; sfb-- ) {
  2766.             int lines;
  2767.             lines = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  2768.             i = 3*sfBandIndex[sfreq].s[sfb] + (j+1) * lines - 1;
  2769.             while ( lines > 0 ) {
  2770.                 if ( xr[1][i/SSLIMIT][i%SSLIMIT] != 0.0 ) {
  2771.                 sfbcnt = sfb;
  2772.                 sfb = -10;
  2773.                 lines = -10;
  2774.                 }
  2775.                 lines--;
  2776.                 i--;
  2777.             }
  2778.             }
  2779.             sfb = sfbcnt + 1;
  2780.             while ( sfb<12 ) {
  2781.             sb = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  2782.             i = 3*sfBandIndex[sfreq].s[sfb] + j * sb;
  2783.             for ( ; sb > 0; sb--) {
  2784.                 is_pos[i] = (*scalefac)[1].s[j][sfb];
  2785.                 if ( is_pos[i] != 7 )
  2786.                 if( lsf ) {
  2787.                     III_i_stereo_k_values(is_pos[i],io,i,k);
  2788.                 } else {
  2789. #ifdef OPTIMIZE
  2790.                     QUICKTAN(is_ratio[i], is_pos[i])
  2791. #else
  2792.                     is_ratio[i] = tan((float)is_pos[i] *
  2793.                             (PI / 12));
  2794. #endif
  2795.                 }
  2796.                 i++;
  2797.             }
  2798.             sfb++;
  2799.             }
  2800.  
  2801.             sb = sfBandIndex[sfreq].s[12]-sfBandIndex[sfreq].s[11];
  2802.             sfb = 3*sfBandIndex[sfreq].s[11] + j * sb;
  2803.             sb = sfBandIndex[sfreq].s[13]-sfBandIndex[sfreq].s[12];
  2804.  
  2805.             i = 3*sfBandIndex[sfreq].s[11] + j * sb;
  2806.             for ( ; sb > 0; sb-- ) {
  2807.             is_pos[i] = is_pos[sfb];
  2808.             is_ratio[i] = is_ratio[sfb];
  2809.             k[0][i] = k[0][sfb];
  2810.             k[1][i] = k[1][sfb];
  2811.             i++;
  2812.             }
  2813.         }
  2814.         }
  2815.     } else {
  2816.         i = 31;
  2817.         ss = 17;
  2818.         sb = 0;
  2819.         while ( i >= 0 ) {
  2820.         if ( xr[1][i][ss] != 0.0 ) {
  2821.             sb = i*18+ss;
  2822.             i = -1;
  2823.         } else {
  2824.             ss--;
  2825.             if ( ss < 0 ) {
  2826.             i--;
  2827.             ss = 17;
  2828.             }
  2829.         }
  2830.         }
  2831.         i = 0;
  2832.         while ( sfBandIndex[sfreq].l[i] <= sb )
  2833.         i++;
  2834.         sfb = i;
  2835.         i = sfBandIndex[sfreq].l[i];
  2836.         for ( ; sfb<21; sfb++ ) {
  2837.         sb = sfBandIndex[sfreq].l[sfb+1] - sfBandIndex[sfreq].l[sfb];
  2838.         for ( ; sb > 0; sb--) {
  2839.             is_pos[i] = (*scalefac)[1].l[sfb];
  2840.             if ( is_pos[i] != 7 )
  2841.             if( lsf ) {
  2842.                 III_i_stereo_k_values(is_pos[i],io,i,k);
  2843.             } else {
  2844. #ifdef OPTIMIZE
  2845.                 QUICKTAN(is_ratio[i], is_pos[i])
  2846. #else
  2847.                 is_ratio[i] = tan((float)is_pos[i] * (PI / 12));
  2848. #endif
  2849.             }
  2850.             i++;
  2851.         }
  2852.         }
  2853.         sfb = sfBandIndex[sfreq].l[20];
  2854.         for ( sb = 576 - sfBandIndex[sfreq].l[21]; sb > 0; sb-- ) {
  2855.         is_pos[i] = is_pos[sfb];
  2856.         is_ratio[i] = is_ratio[sfb];
  2857.         k[0][i] = k[0][sfb];
  2858.         k[1][i] = k[1][sfb];
  2859.         i++;
  2860.         }
  2861.     }
  2862.     }
  2863.  
  2864.     for (ch = 0; ch < 2; ch++)
  2865.     for (sb = 0; sb < SBLIMIT; sb++)
  2866.         for (ss = 0; ss < SSLIMIT; ss++)
  2867.         lr[ch][sb][ss] = 0;
  2868.  
  2869.     if (stereo == 2)
  2870.     for(sb = 0; sb < SBLIMIT; sb++)
  2871.         for(ss = 0; ss < SSLIMIT; ss++) {
  2872.         i = (sb*18)+ss;
  2873.         if ( is_pos[i] == 7 ) {
  2874.             if ( ms_stereo ) {
  2875.             lr[0][sb][ss] = (xr[0][sb][ss]+xr[1][sb][ss])/1.41421356;
  2876.             lr[1][sb][ss] = (xr[0][sb][ss]-xr[1][sb][ss])/1.41421356;
  2877.             } else {
  2878.             lr[0][sb][ss] = xr[0][sb][ss];
  2879.             lr[1][sb][ss] = xr[1][sb][ss];
  2880.             }
  2881.         } else if (i_stereo ) {
  2882.             if ( lsf ) {
  2883.             lr[0][sb][ss] = xr[0][sb][ss] * k[0][i];
  2884.             lr[1][sb][ss] = xr[0][sb][ss] * k[1][i];
  2885.             } else {
  2886.             lr[0][sb][ss] = xr[0][sb][ss] * (is_ratio[i]/(1+is_ratio[i]));
  2887.             lr[1][sb][ss] = xr[0][sb][ss] * (1/(1+is_ratio[i]));
  2888.             }
  2889.         } else {
  2890.             fprintf(stderr, "Error in stereo processing\n");
  2891.         }
  2892.         }
  2893.  
  2894.     else    /* mono , bypass xr[0][][] to lr[0][][]*/
  2895.  
  2896.     for (sb = 0; sb < SBLIMIT; sb++)
  2897.         for(ss = 0; ss < SSLIMIT; ss++)
  2898.         lr[0][sb][ss] = xr[0][sb][ss];
  2899. }
  2900.  
  2901. REAL Ci[8] = {-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
  2902.  
  2903.  
  2904. void III_antialias(
  2905.     REAL         xr[SBLIMIT][SSLIMIT],
  2906.     REAL         hybridIn[SBLIMIT][SSLIMIT],
  2907.     struct gr_info_s    *gr_info,
  2908.     frame_params    *fr_ps)
  2909. {
  2910.     static int      init = 1;
  2911.     static REAL      ca[8], cs[8];
  2912.     REAL      bu, bd;    /* upper and lower butterfly inputs */
  2913.     int       ss, sb, sblim;
  2914.  
  2915.     if (init == 1) {
  2916.     int    i;
  2917.     REAL    sq;
  2918.     for (i = 0; i < 8; i++) {
  2919.         sq = sqrt(1.0 + Ci[i] * Ci[i]);
  2920.         cs[i] = 1.0 / sq;
  2921.         ca[i] = Ci[i] / sq;
  2922.     }
  2923.     init = 0;
  2924.     }
  2925.  
  2926.     /* Clear all inputs */
  2927.     for(sb=0;sb<SBLIMIT;sb++)
  2928.     for(ss=0;ss<SSLIMIT;ss++)
  2929.         hybridIn[sb][ss] = xr[sb][ss];
  2930.  
  2931.     if  (gr_info->window_switching_flag && (gr_info->block_type == 2) &&
  2932.             !gr_info->mixed_block_flag )
  2933.     return;
  2934.  
  2935.     if ( gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  2936.             (gr_info->block_type == 2))
  2937.     sblim = 1;
  2938.     else
  2939.     sblim = SBLIMIT-1;
  2940.  
  2941.     /*
  2942.      * 31 alias-reduction operations between each pair of sub-bands
  2943.      * with 8 butterflies between each pair
  2944.      */
  2945.     for (sb = 0; sb < sblim; sb++)
  2946.     for (ss = 0; ss < 8; ss++) {
  2947.         bu = xr[sb][17-ss];
  2948.         bd = xr[sb+1][ss];
  2949.         hybridIn[sb][17-ss] = (bu * cs[ss]) - (bd * ca[ss]);
  2950.         hybridIn[sb+1][ss] = (bd * cs[ss]) + (bu * ca[ss]);
  2951.     }
  2952. }
  2953.  
  2954.  
  2955. /*------------------------------------------------------------------*
  2956.  *                                    *
  2957.  *    Function: Calculation of the inverse MDCT             *
  2958.  *    In the case of short blocks the 3 output vectors are already  *
  2959.  *    overlapped and added in this module.                *
  2960.  *                                    *
  2961.  *    New layer3                                                    *
  2962.  *                                    *
  2963.  *------------------------------------------------------------------*/
  2964. void inv_mdct(
  2965.     REAL        in[18],
  2966.     REAL        out[36],
  2967.     int            block_type)
  2968. {
  2969.     int            i, m, N, p;
  2970.     static REAL        win[4][36];
  2971.     static int        init = 1;
  2972.     REAL        sum;
  2973. #ifdef OPTIMIZE
  2974.     static REAL        const1[12][6];
  2975.     static REAL        const3[36][18];
  2976. #else
  2977.     REAL        tmp[12];
  2978.     static REAL        COS[4 * 36];
  2979. #endif
  2980.  
  2981.     if (init == 1) {
  2982.  
  2983.     /* Type 0 */
  2984.     for (i = 0; i < 36; i++)
  2985.         win[0][i] = sin(PI36 * (i + 0.5));
  2986.  
  2987.     /* Type 1 */
  2988.     for (i = 0; i < 18; i++)
  2989.         win[1][i] = sin(PI36 * (i + 0.5));
  2990.     for (i = 18; i < 24; i++)
  2991.         win[1][i] = 1.0;
  2992.     for (i = 24; i < 30; i++)
  2993.         win[1][i] = sin(PI12 * (i + 0.5 - 18));
  2994.     for (i = 30; i < 36; i++)
  2995.         win[1][i] = 0.0;
  2996.  
  2997.     /* Type 3 */
  2998.     for (i = 0; i < 6; i++)
  2999.         win[3][i] = 0.0;
  3000.     for (i = 6; i < 12; i++)
  3001.         win[3][i] = sin(PI12 * (i + 0.5 - 6));
  3002.     for (i = 12; i < 18; i++)
  3003.         win[3][i] = 1.0;
  3004.     for (i = 18; i < 36; i++)
  3005.         win[3][i] = sin(PI36 * (i + 0.5));
  3006.  
  3007.     /* Type 2 */
  3008.     for (i = 0; i < 12; i++)
  3009.         win[2][i] = sin(PI12 * (i + 0.5)) ;
  3010.     for (i = 12; i < 36; i++)
  3011.         win[2][i] = 0.0 ;
  3012.  
  3013. #ifdef OPTIMIZE
  3014.  
  3015.     /* Pre-calculate cosine array */
  3016.     N = 12;
  3017.     for (p = 0; p < N; p++)
  3018.         for (m = 0; m < N/2; m++)
  3019.         const1[p][m] = cos((PI/(2*N)) * (2*p+1+N/2) * (2*m+1));
  3020.  
  3021.     /*
  3022.      *  Create an optimized cosine lookup table
  3023.      *  for the block_type != 2 case
  3024.      */
  3025.     for (p = 0; p < 36; p++) {
  3026.         int pconst = (p * 2) + 19;
  3027.  
  3028.         for (m = 0; m < 18; m++)
  3029.         /* Calculate cosine value for this position */
  3030.         const3[p][m] = cos(PI/(2*36) *
  3031.                   ((pconst * ((m << 1) + 1)) % 144));
  3032.     }
  3033.     init = 0;
  3034.     }
  3035.  
  3036.     if (block_type == 2) {
  3037.  
  3038.     /* Clear the out[] array */
  3039.     memset((void *)&out[0], 0, sizeof(REAL) * 36);
  3040.  
  3041.     /*
  3042.      * Optimization I
  3043.      *
  3044.      * for (i = 0; i < 3; i++) {
  3045.      *    for (p = 0; p < 12; p++) {
  3046.      *    sum = in[i] * const1[p][0] +
  3047.      *        in[i + 3] * const1[p][1] +
  3048.      *        in[i + 6] * const1[p][2] +
  3049.      *        in[i + 9] * const1[p][3] +
  3050.      *        in[i + 12] * const1[p][4] +
  3051.      *        in[i + 15] * const1[p][5];
  3052.      *    out[6 * i + p + 6] += sum * win[2][p];
  3053.      *    }
  3054.      * }
  3055.      */
  3056.  
  3057.     /*
  3058.      * Optimization II
  3059.      */
  3060.     for (p = 0; p < 12; p++) {
  3061.         sum = in[0] * const1[p][0] +
  3062.         in[3] * const1[p][1] +
  3063.         in[6] * const1[p][2] +
  3064.         in[9] * const1[p][3] +
  3065.         in[12] * const1[p][4] +
  3066.         in[15] * const1[p][5];
  3067.         out[p + 6] += sum * win[2][p];
  3068.     }
  3069.     for (p = 0; p < 12; p++) {
  3070.         sum = in[1] * const1[p][0] +
  3071.         in[4] * const1[p][1] +
  3072.         in[7] * const1[p][2] +
  3073.         in[10] * const1[p][3] +
  3074.         in[13] * const1[p][4] +
  3075.         in[16] * const1[p][5];
  3076.         out[p + 12] += sum * win[2][p];
  3077.     }
  3078.     for (p = 0; p < 12; p++) {
  3079.         sum = in[2] * const1[p][0] +
  3080.         in[5] * const1[p][1] +
  3081.         in[8] * const1[p][2] +
  3082.         in[11] * const1[p][3] +
  3083.         in[14] * const1[p][4] +
  3084.         in[17] * const1[p][5];
  3085.         out[p + 18] += sum * win[2][p];
  3086.     }
  3087.  
  3088.     } else { /* block_type != 2 */
  3089.  
  3090.     /*
  3091.      * Optimization I
  3092.      *
  3093.      * for (p = 0; p < 36; p++) {
  3094.      *    sum = 0.0;
  3095.      *
  3096.      *    for (m = 0; m < 18; m++)
  3097.      *     sum += in[m] * const3[p][m];
  3098.      *    out[p] = sum * win[block_type][p];
  3099.      * }
  3100.      */
  3101.  
  3102.     /*
  3103.      * Optimization II
  3104.      */
  3105.     int nzcount, nzin[18];
  3106.  
  3107.     for (p = 0, nzcount = 0; p < 18; p++)
  3108.         if (in[p] < -0.0004 || in[p] > 0.0004)    /* analyze input */
  3109.         nzin[nzcount++] = p;
  3110.  
  3111.     for (p = 0; p < 36; p++) {
  3112.         sum = 0.0;
  3113.  
  3114.         for (m = 0; m < nzcount; m++)
  3115.         sum += in[nzin[m]] * const3[p][nzin[m]];
  3116.         out[p] = sum * win[block_type][p];
  3117.     }
  3118.     }
  3119.  
  3120. #else /* original code */
  3121.  
  3122.     for (i = 0; i < 4*36; i++)
  3123.         COS[i] = cos(PI/(2*36) * i);
  3124.  
  3125.     init = 0;
  3126.     }
  3127.  
  3128.     for (i = 0; i < 36; i++)
  3129.     out[i]=0;
  3130.  
  3131.     if (block_type == 2){
  3132.     N = 12;
  3133.     for (i=0; i<3; i++){
  3134.         for (p = 0; p<N; p++){
  3135.         sum = 0.0;
  3136.         for(m=0; m<N/2; m++)
  3137.             sum += in[i+3*m] * cos( PI/(2*N)*(2*p+1+N/2)*(2*m+1) );
  3138.         tmp[p] = sum * win[block_type][p] ;
  3139.         }
  3140.         for (p=0; p<N; p++)
  3141.         out[6*i+p+6] += tmp[p];
  3142.        }
  3143.     } else {
  3144.     N = 36;
  3145.     for (p=0; p<N; p++){
  3146.         sum = 0.0;
  3147.         for (m=0; m < N/2; m++)
  3148.         sum += in[m] * COS[((2*p+1+N/2)*(2*m+1))%(4*36)];
  3149.         out[p] = sum * win[block_type][p];
  3150.     }
  3151.     }
  3152. #endif /* OPTIMIZE */
  3153. }
  3154.  
  3155.  
  3156.  
  3157. void III_hybrid(
  3158.     REAL         fsIn[SSLIMIT],   /* freq samples per subband in */
  3159.     REAL         tsOut[SSLIMIT],  /* time samples per subband out */
  3160.     int             sb,
  3161.     int             ch,
  3162.     struct gr_info_s    *gr_info,
  3163.     frame_params    *fr_ps)
  3164. {
  3165.     int         ss;
  3166. #ifdef OPTIMIZE
  3167.     static REAL     rawout[36];
  3168. #else
  3169.     REAL     rawout[36];
  3170. #endif
  3171.     static REAL     prevblck[2][SBLIMIT][SSLIMIT];
  3172.     static int     init = 1;
  3173.     int         bt;
  3174.  
  3175.     if (init == 1) {
  3176.     int i, j, k;
  3177.  
  3178.     for (i = 0; i < 2; i++)
  3179.         for (j = 0; j < SBLIMIT; j++)
  3180.         for (k = 0; k < SSLIMIT; k++)
  3181.             prevblck[i][j][k] = 0.0;
  3182.     init = 0;
  3183.     }
  3184.  
  3185.     bt = (gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  3186.         (sb < 2)) ? 0 : gr_info->block_type;
  3187.  
  3188.     inv_mdct(fsIn, rawout, bt);
  3189.  
  3190.     /* Overlap addition */
  3191.     for (ss = 0; ss < SSLIMIT; ss++) {        /* 18 */
  3192.     tsOut[ss] = rawout[ss] + prevblck[ch][sb][ss];
  3193.     prevblck[ch][sb][ss] = rawout[ss+18];
  3194.     }
  3195. }
  3196.  
  3197.  
  3198. /* Return the number of slots for main data of current frame */
  3199.  
  3200. int main_data_slots(frame_params fr_ps)
  3201. {
  3202.     int nSlots =
  3203.     (144 * bitrate[fr_ps.header->version][2][fr_ps.header->bitrate_index]) /
  3204.     s_freq[fr_ps.header->version][fr_ps.header->sampling_frequency];
  3205.  
  3206.     if (fr_ps.header->version != MPEG_PHASE2_LSF) {
  3207.     if (fr_ps.stereo == 1)
  3208.         nSlots -= 17;
  3209.     else
  3210.         nSlots -=32;
  3211.     } else {
  3212.     nSlots = nSlots / 2;
  3213.     if (fr_ps.stereo == 1)
  3214.         nSlots -= 9;
  3215.     else
  3216.         nSlots -=17;
  3217.     }
  3218.  
  3219.     if (fr_ps.header->padding)
  3220.     nSlots++;
  3221.     nSlots -= 4;
  3222.     if (fr_ps.header->error_protection)
  3223.     nSlots -= 2;
  3224.  
  3225.     return nSlots;
  3226. }
  3227.